POJ 1001:Exponentiation —— 高精度浮点数运算

POJ1001 (求高精度幂,Exponentiation,C++) 北京大学程序在线评测系统 (“PekingUniversityOnlineJudge”) POJ1001 (求高精度幂,Exponentiation,C++) 题目的网址 题目大意是对一个实数R( 0.0 < R < 99.999 ),精确计算 R 的 n 次方,其中n 是整数并且 0 < n <= 25。 具体思路在代码中的注释中都有体现,不做过多解释。 话不多说,具体代... 阅读详情

总时间限制: 500ms 内存限制: 65536kB
描述
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems.

This problem requires that you write a program to compute the exact value of Rnwhere R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

输入
The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

输出
The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.
样例输入
95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

样例输出
548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

自己的代码:超时
思路:先将浮点数转化成整数,然后做整数大整数乘法,最后处理小数点

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

string r;
int n;

string multiply(string mynum1, string mynum2)
{
    //i 和 j 相乘的结果是 res 的 (i+j) 位
    int it1 = 0, it2 = 0;
    //预处理,去掉前导 0
    while(mynum1[it1] == '0')
        it1++;
    while(mynum2[it2] == '0')
        it2++;

    string num1 = "", num2 = "";
    while(it1 != mynum1.length())
        num1 += mynum1[it1++];
    while(it2 != mynum2.length())
        num2 += mynum2[it2++];

    string str_res = "";
    int first = 0;
    int res[320] = {};
    int len1 = num1.length(), len2 = num2.length();
    if(len1 < len2)
        return multiply(num2, num1);
    int a[160] = {};
    int b[160] = {};

    for(int i=0;i<len1;i++)
        a[i] = num1[len1-i-1]-'0';
    for(int i=0;i<len2;i++)
        b[i] = num2[len2-i-1]-'0';

    for(int i=0;i<len1;i++)
    {
        for(int j=0;j<len2;j++)
        {
            res[i+j] += a[i]*b[j];
        }
    }

    for(int i=0;i<len1+len2+2;i++)
    {
        if(res[i] >= 10)
        {
            res[i+1] += res[i]/10;
            res[i] %= 10;
        }
    }

    for(int i=315;i>=0;i--)
        if(res[i] != 0)
        {
            first = i; break;
        }
    for(int i=first;i>=0;i--)
    {
        str_res += res[i]+'0';
    }

    return str_res;
}

string multi(string a,string b)     //浮点数乘法
{
    int PointA = 0, PointB = 0;
    int flag_zero = 0;
    string NewA = "", NewB = "";
    string res = "";        //存放最后结果
    int lena = a.length(), lenb = b.length();

    if(a[0]=='0' || b[0]=='0')
        flag_zero = 1;

    for(int i=0;i<lena;i++)
    {
        if(a[i] == '.')
        {
            PointA = i; break;
        }
    }
    for(int i=0;i<lenb;i++)
    {
        if(b[i] == '.')
        {
            PointB = i; break;
        }
    }
    //转换成两个大整数的乘法
    for(int i=0;i<lena;i++)
        if(i != PointA)
            NewA += a[i];
    for(int i=0;i<lenb;i++)
        if(i != PointB)
            NewB += b[i];

    string tmp_res = multiply(NewA, NewB);
    int it = tmp_res.length()-1;
    int ZeroNum = 0;

    //计算结果中后缀 0 的数目
    while(tmp_res[it] == '0')
    {
        ZeroNum++; it--;
    }
    int add_zero_num = 0;
    int ttt = lena+lenb-PointA-PointB-2-ZeroNum;
    while(ttt > 0)
    {
        ttt--;
        if(it >= 0)
            res += tmp_res[it--];
        else        //需要添加零
        {
            ttt++;
            break;
        }

    }
    //补 0
    while(ttt--)
        res += '0';
    //添加小数点
    res += '.';
    if(flag_zero)
        res += '0';
    else
    {
        while(it >= 0)
        {
            res += tmp_res[it--];
        }
    }

    //翻转结果
    reverse(res.begin(), res.end());

    return res;
}

string cal(string r,int n)     //calculate r^n,二分法
{
    if(n == 1)
        return r;
    else
    {
        if(n%2 == 0)
            return multi(cal(r,n/2),cal(r,n/2));
        else
            return multi(r,cal(cal(r,n/2),2));
    }
}

int main()
{
    while(cin>>r>>n)
    {
        cout<<cal(r,n)<<endl;
    }
    return 0;
}

1 ms AC代码:在处理大整数乘法时用数学思想做了优化。

#include <stdio.h>
#include <string.h>

int len; // total length of exponentiation result
int product[126] = {0}; // storing result, at most length 5*25 + 1 = 126

void multiply(int a[], int n)
{
    int i;
    int carry = 0; // a carry number in multiplying
    for (i = 0; i < len; i++)
    {
        int temp = a[i]*n + carry;
        a[i] = temp % 10;
        carry = temp / 10;      
    }
    while (carry)
    {
        a[i++] = carry % 10;
        carry /= 10;
    }
    len = i;
}

int main(int argc, char* argv[])
{
    int n;  // power n
    char s[6]; // real number R, at most the length is 6
    while (scanf("%s %d", s, &n) != EOF)
    {
        int position=0, i=0, num=0, j=0;
        for (i=0; i<strlen(s); i++) 
        {
            if (s[i] == '.')
            {
                position = (strlen(s) - 1 - i) * n; // calculate decimal point position after R^n
            }
            else
            {
                num = num*10 + s[i] - 48; // 将浮点数转化成整数
            }       
        }

        // product calculation 
        product[0]=1;
        len = 1;
        // 获得幂
        for (i = 0; i < n; i++)
        {
            multiply(product, num);
        }

        // format output
        if (len <= position) // product is less than 1
        {
            printf("."); // print decimal point
            for (i=0; i<position-len; i++)
            {
                printf("0"); // print zero between decimal point and decimal
            }

            j = 0;
            //while (product[j] == 0) // trim trailing zeros
            //{
            //    j++;
            //}
            for (i=len-1; i>=j; i--)
            {
                printf("%d", product[i]);
            }
        }   
        else
        {
            j=0;
            while (product[j]==0 && j<position) // trim trailing zeros
            {
                j++;
            }
            for (i=len-1; i>=j; i--)
            {
                if (i+1 == position) // cause index in C language starts from 0
                {
                    printf(".");
                }
                printf("%d", product[i]);
            }
        }
        printf("\n");
    }
}
【POJ 1001】Exponentiation 题解&代码(c++) poj,高精度, 阅读详情

相关推荐

C语言高精度浮点计算代码

【问题描述】 计算 k *∑(x^i), -m ≤ i ≤ n,精确到小数点后14位(最后要求四舍五入)。 【输入形式】 从文件sigma.in中顺序读入4个由空格分隔的正整数k、x、m、n。(1≤ k、x、m、n ≤ 100)。 【输出形式】 将结果写到标准输出,其中小数占14位,不足部分补零,最后输出一个回车。(输入数据保证输出结果小于2^53。) 【输入样例】 1 10 5 5 【输出样例】 111111.11111000000000

python 高精度整数、高精度浮点数及其相关运算

python 高精度整数、高精度浮点数及其相关运算

qq_51184727的博客 1万+

高精度浮点数计算器

利用AT89C52单片机制作高精度浮点数计算器,LCD1602两行显示,8279扫描键盘,扩展外部静态RAM6264,附赠课程设计报告书,个人觉得很不错,所以拿出来分享给大家!压缩包有仿真和代码,附带设计报告书和运行截图等!

高精度乘法计算

高精度乘法计算基础 1.高精度浮点运算方法   高精度浮点(Floating Point,FP)运算可以转换成整数型运算。由于高精度浮点数可以看成是由整数部分(Integer Part,IP)与小数部分(Decimal Part,DP)的组合,因此其乘法可以看成以下3种运算的组合,即整数x整数(IxI)、整数x小数(IxD)和小数x小数(DxD)。用表达式表示,则FP1*FP2=IP1*IP2...

weixin_30634661的博客 517

poj1001源码

poj1001源码,c语言版,通过ac,包含注释,容易懂

保姆级教程:用Docker快速部署mpromonet/webrtc-streamer实现WebRTC直播(附常见问题解决)

本文提供了一份详细的Docker部署mpromonet/webrtc-streamer的实战指南,帮助用户快速搭建WebRTC直播服务。内容涵盖从环境准备、基础部署到进阶配置与性能调优的全过程,并针对容器网络、视频流配置、跨网访问等常见问题提供了解决方案,旨在实现低延迟的流媒体传输。

dell8的博客 177

PHP 浮点数高精度运算

概述 记录下,工作中遇到的坑 ... 关于 PHP 浮点数运算,特别是金融行业、电子商务订单管理、数据报表等相关业务,利用浮点数进行加减乘除时,稍不留神运算结果就会出现偏差,轻则损失几十万,重则会有信誉损失,甚至吃上官司,我们一定要引起高度重视! 浮点数运算的“锅” //加 $a = 0.1; $b = 0.7; $c = intval(($a + $b) * 10); echo $c."<br>"; //输出:7 //减 $a = 100; $b = 99.98; $c = ...

曾沂宏的博客 787

Ansys Lumerical 2023 R1.2新功能实战:薄膜铌酸锂调制器仿真全流程解析

本文详细解析了Ansys Lumerical 2023 R1.2在薄膜铌酸锂电光相位调制器仿真中的最新功能与应用。通过实战演示,展示了从电极配置、电场计算到模式分析的全流程,重点介绍了各向异性介电常数支持和多物理场耦合优化等关键技术,助力开发者高效设计高性能光通信器件。

weixin_28454475的博客 1218

精确的浮点数运算

你需要对浮点数执行精确的计算操作,并且不希望有任何小误差的出现。浮点数的一个普遍问题是它们并不能精确的表示十进制数。并且,即使是最简单的数学运算也会产生小的误差,比如: a = 3.2 b = 5.4 print(a + b) print((a + b) == 8.6) 运行结果如下: 8.600000000000001 False 这些错误是由底层CPU和IEEE 754标准通过自己的浮点单位去...

阳神 1146

windows 下AD9361的 no-os master 工程搭建

参考网站: https://wiki.analog.com/resources/fpga/docs/build#windows_environment_setup https://wiki.analog.com/resources/eval/user-guides/ad-fmcomms2-ebz/software/no-os-setup 参考博客: https://blog.youkuaiyun.com...

Bronceyang的博客 7295

1.47寸ST7789V3彩屏硬件设计与驱动调试指南

TFT LCD是一种基于薄膜晶体管阵列控制像素明暗的主动式显示技术,其核心依赖于专用驱动IC(如ST7789V3)完成时序生成、GRAM管理与伽马校正。该类小尺寸彩色显示屏广泛应用于嵌入式HMI、IoT终端与可穿戴设备,具备高PPI、低功耗和SPI接口易集成等工程优势。理解RGB565数据格式、SPI Mode 0通信时序及关键寄存器(如0x36方向控制、0x3A像素格式、0x2A/0x2B地址窗口)是实现稳定显示的基础;而正确处理电源设计(严禁5V直供)、复位时序与背光PWM调光,则直接决定系统可靠性与能

weixin_30995917的博客 166

高精度浮点数运算

本程序实现了高精度浮点数的加法,减法,乘法,乘方和除法运算,有效数字精确到MAX。 为了便于进位,本程序采用了较为独特的数据结构,即把浮点数分成整数和小数部分,分别存储在两个不同的数组中。 其中整数部分数字存储在ValInt[MAX-lenInt...MAX) ,小数部分数字存储在ValDec[1...lenDec],ValDec[0]用来存储进位或借位。 这样在计算中补齐0的时候不需要移动数组元素,只需移动下标即可,大大提升了效率。

QiaoRuoZhuo的专栏 6740

基于C++和Qt开发IEC104规约主站调试软件(第二章)

本章节主要讲解iec104主站软件界面设计和从站的连接功能以及部分功能代码的展示。

半青年的博客 862

高精度浮点数计算 poj 1001 Exponentiation

poj 1001 Exponentiation Time Limit Memory Limit Total Submissions Accepted 500ms 10000k 185164 44572 Descrimination   Problems involving the computation of exact values of very large magnitu...

weixin_40215124的博客 503

相机标定(六)—— 张正友标定法

相机标定一、相机标定的目的1. 相机的成像过程2. 相机标定的目的3. 畸变与畸变矫正二、张正友标定法简介三、标定相机内外参1. 求解内参和外参的积2. 求解内参矩阵3. 求解外参矩阵四、标定相机的畸变参数五、L-M算法参数优化六、相机标定的步骤七、源代码 一、相机标定的目的 1. 相机的成像过程 2. 相机标定的目的 3. 畸变与畸变矫正 二、张正友标定法简介 三、标定相机内外参 1. 求解内参和外参的积 2. 求解内参矩阵 3. 求解外参矩阵 四、标定相机的畸变参数 五、L-M算法参数优化 六、相机

weixin_41480034的博客 1万+

爬取Unsplash高清壁纸:Python爬虫实战教程(最新技术)

在当今数字化时代,高清壁纸已经成为人们个性化设备的重要元素。Unsplash作为全球最知名的免费高质量图片网站,汇集了数百万张由全球摄影师贡献的高清图片。然而,Unsplash官网的下载方式需要手动操作,对于需要批量下载壁纸的用户来说效率较低。本文将带你深入探索如何使用Python爬虫技术高效爬取Unsplash高清壁纸,从基础到进阶,全面掌握图片爬取的核心技术。Unsplash成立于2013年,是一个完全免费的图片分享平台。高质量内容:所有图片均由专业摄影师上传,分辨率普遍达到4K以上免费商用。

2201_76125261的博客 937

POJ 1001 Exponentiation - 高精度高位数运算

/* * POJ 1001 Exponentiation * 求R^n, 其中0.0 < R < 99.999, 0 < n <= 25; * * 1) 高精度高位数计算; * 2) 将字符串转换为数组,按位计算; * 3) 采用连乘求幂 或 将次数转换为二进制数以减少相乘次数; * */#include using namespace std;#define MAX_NUMBER_LENGTH

ChenK在学习! 1005

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

高精度浮点数幂次方 POJ 1001 Exponentiation

题目是在POJ上的第1001道 Exponentiation Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt

MysticalRzc的博客 1063

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

POJ1001 Exponentiation【高精度乘方】

题目: Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for

淡然坊 1620

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

POJ 1001 Exponentiation

POJ 1001 Exponentiation 心血来潮花了一上午做了这道恶心死人的高精度。展示一下打了各种补丁的程序。 type gj=record len:longint; data:array[1..1000]of longi...

aimifeng9177的博客 179

【北大poj】1001 Exponentiation

北大poj 1001 Exponentiation Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing...

red_velvet_cake的博客 292

POJ解题报告——1001 Exponentiation

POJ解题报告——1001 Exponentiation问题描述求高精度幂,输入是一个实数 R (0.0 < R < 99.999) 和 指数 n (0 < n <= 25), 要求精确计算 R 的 n 次方(Rn)。如 95.123 12 结果为548815620517731830194541.899025343415715973535967221869852721详见 http://poj.

drop_k的博客 509

POJ1001 Exponentiation 高精度

求高精度幂 Description 对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。  现在要你解决的问题是:对一个实数R( 0.0 &lt; R &lt; 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0 &lt; n &lt;= 25。 Input T输入包括多组 R 和 n。 R 的值占第 1 到...

Yerkeys的博客 262

POJ 1001 Exponentiation 高精度小数相乘

好吧,又一轮刷POJ,之前欠下的债现在还上,前面还有个1001没做,看着摆在那总觉得不舒服= =虽然觉得麻烦......好吧,终究还是要写的。     题意很简单,就是给你一个小数,然后求他的n次幂,麻烦就麻烦在高精度的还要写小数= =高精度的模板之前倒是打过,不过这个是小数,所以还要把对小数的处理加上。写这一步倒是花的时间比我预料中的要少,毕竟小数相乘,先处理好一开始的小数点后面有几位。最后结

富士山下 542

[POJ][1001]Exponentiation

Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many comp

软件工程学森 1万+
上一篇: LeetCode | 61. Rotate List
下一篇: LeetCode | 62. Unique Paths
PKU_CXK
博客等级 码龄10年 87粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值