OpenJudge Exponentiation(指数计算)

目录

Exponentiation

要求:

描述: 

输入: 

输出: 

样例输入: 

样例输出: 

 问题分析:

优化:

最终代码:

总结:


​​​​​​​

Exponentiation

要求:

总时间限制: 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

 问题分析:

(还是希望可以直接看英文原题)

我们需要处理的问题是小数的指数运算,当然这个题不可能简单地让我们用\text{pow}函数来进行直接运算,通过样例我们可以看到,本题中出现的数据都是非常大的,需要高精度计算(大数字加法、大数字乘法…).本题对指数运算进行了简化,小数只占前6列,整数只占8、9列.但是有三个要求:1)Leading zeros should be suppressed in the output.(整数部分如果是0则删去); 2)Insignificant trailing zeros must not be printed.(不重要的小数点后末尾0应删去); 3)Don't print the decimal point if the result is an integer. (如果结果是整数不要输出小数点).

一个直接的想法就是,将给定的实数R乘上n次即可,这种算法需要对高精度乘法与高精度加法十分熟悉。高精度乘法与高精度加法的本质就是竖式计算。不难看出,在进行高精度乘法的时候,中间是需要高精度加法的,所以我们先来解决高精度加法。(这里考虑使用string比较方便)

string HugePlus(string a,string b){
    if(a.size()<b.size()){
        swap(a,b);
    }//使a的位数更大
    int flag=0;//flag表示进位
    string temp;//temp表示结果
    reverse(a.begin(),a.end());reverse(b.begin(),b.end());//倒转,方便竖式计算
    while(b.size()<a.size()) {
        b.push_back('0');
    }
    int i=0;
    while(i<a.size()){
        temp.push_back( ((a[i] - '0' + b[i] - '0' + flag) % 10 + '0')) ;//具体计算过程
        flag = (a[i] - '0' + b[i] - '0' + flag) / 10;
        i++;
    }
    if(flag!=0){
        temp.push_back(flag+'0');//如果计算完后还有进位,就再添一位
    }
    reverse(temp.begin(),temp.end());//翻转回来
    return temp;
}

在这个高精度加法的基础之上,我们可以写出高精度乘法:

string HugeTimes(string a,string b){
    string temp1="0";string temp2;//高精度乘法中需要进行若干次加法
    reverse(a.begin(),a.end());reverse(b.begin(),b.end());
    for(int i=0;i<a.size();i++){
        int flag=0;
        temp2.clear();//第二个加数用完需要清空
        for(int j=0;j<b.size();j++){
            temp2.push_back(((b[j]-'0')*(a[i]-'0')+flag)%10+'0');//乘法过程
            flag=((b[j]-'0')*(a[i]-'0')+flag)/10;
        }
        if(flag!=0){
            temp2.push_back(flag+'0');
        }
        reverse(temp2.begin(),temp2.end());
        for(int k=0;k<i;k++) {
            temp2.push_back('0');//乘法的竖式计算中,我们用添加末尾0来模拟加数前移
        }
        temp1= HugePlus(temp1,temp2);
    }
    return temp1;//可以自己尝试一下竖式乘法过程,加深体会
}

我们的工作看似已经完成了,但是实际上还需要一些处理。题目中给出的R是包含小数点的,上面的高精度计算是不含小数点的,所以我们需要对小数点进行处理。而我们知道,n位小数与m位小数相乘的结果必有m\times n位小数(包含末尾0),所以我们在进行指数运算的时候不妨将其看作整数,最后输出时再添加小数点即可。

优化:

对于指数运算,我们通常可以考虑利用二分的思想来进行优化:比如计算2^88次方时,我们只需计算出4次方再平方;要计算4次方,只需计算出2次方再平方……;计算9次方时,只需计算出4次方,平方后再乘自身即可。这样会大幅度减少高精度计算的次数(不用一个一个乘了)。

string Exponentiation(string a,int x){
    if(x%2==0){
        return HugeTimes(Exponentiation(a,x/2), Exponentiation(a,x/2));
    }
    else if(x%2!=0){
        if(x==1) return a;
        else return HugeTimes(HugeTimes(Exponentiation(a,x/2), Exponentiation(a,x/2)),a);
    }
}

最终代码:

#include <iostream>
#include<algorithm>
using namespace std;
string HugePlus(string a,string b){
    if(a.size()<b.size()){
        swap(a,b);
    }
    int flag=0;string temp;
    reverse(a.begin(),a.end());reverse(b.begin(),b.end());
    while(b.size()<a.size()) {
        b.push_back('0');
    }
    int i=0;
    while(i<a.size()){
        temp.push_back( ((a[i] - '0' + b[i] - '0' + flag) % 10 + '0')) ;//NOLINT
        flag = (a[i] - '0' + b[i] - '0' + flag) / 10;
        i++;
    }
    if(flag!=0){
        temp.push_back(flag+'0');
    }
    reverse(temp.begin(),temp.end());
    return temp;
}
string HugeTimes(string a,string b){
    string temp1="0";string temp2;
    reverse(a.begin(),a.end());reverse(b.begin(),b.end());
    for(int i=0;i<a.size();i++){
        int flag=0;
        temp2.clear();
        for(int j=0;j<b.size();j++){
            temp2.push_back(((b[j]-'0')*(a[i]-'0')+flag)%10+'0');//NOLINT
            flag=((b[j]-'0')*(a[i]-'0')+flag)/10;
        }
        if(flag!=0){
            temp2.push_back(flag+'0');
        }
        reverse(temp2.begin(),temp2.end());
        for(int k=0;k<i;k++) {
            temp2.push_back('0');
        }
        temp1= HugePlus(temp1,temp2);
    }
    return temp1;
}
string Exponentiation(string a,int x){
    if(x%2==0){
        return HugeTimes(Exponentiation(a,x/2), Exponentiation(a,x/2));
    }
    else if(x%2!=0){
        if(x==1) return a;
        else return HugeTimes(HugeTimes(Exponentiation(a,x/2), Exponentiation(a,x/2)),a);
    }
}
int main() {
    string R;
    int n;
    while(cin>>R>>n){
        int point=R.find('.');
        R.erase(R.begin()+point);//删除小数点
        point=5-point;
        point*=n;//计算出最后有多少位小数
        string temp= Exponentiation(R,n);
        temp.insert(temp.length()-point,".");
        if(temp[0]=='0'&&temp[1]=='.')
            temp.erase(temp.begin());//如果整数部分是0,就删掉
        for(int i=temp.size()-1;;i--){
            if(temp[i]=='0') temp.erase(temp.end()-1);
            else if(temp[i]=='.'){
                temp.erase(temp.end()-1);
                break;
            }
            else break;
        }//删去末尾0和不需要的小数点
        printf("%s\n",temp.c_str());//输出,也可以用cout<<temp<<endl;
    }
    return 0;
}

总结:

本题实际上还可以进行一些小优化,比如:如果 R< 1 , 则可以只计算小数部分;在指数二分分递归的时候使用记忆化存储……但是笔者认为这些优化影响不大,本题的数据也不需要对时间太过在意,关键是对高精度计算的掌握。如果这篇文章对您有帮助的话,请点赞关注哦!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值