求高精度幂-----Exponentiation
2014-03-19 20:19
176 查看
Description
对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。
现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0 < n <= 25。
Input
T输入包括多组 R 和 n。 R 的值占第 1 到第 6 列,n 的值占第 8 和第 9 列。
Output
对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方。输出需要去掉前导的 0 后不要的 0 。如果输出是整数,不要输出小数点。
Sample Input
Sample Output
对数值很大、精度很高的数进行高精度计算是一类十分常见的问题。比如,对国债进行计算就是属于这类问题。
现在要你解决的问题是:对一个实数R( 0.0 < R < 99.999 ),要求写程序精确计算 R 的 n 次方(Rn),其中n 是整数并且 0 < n <= 25。
Input
T输入包括多组 R 和 n。 R 的值占第 1 到第 6 列,n 的值占第 8 和第 9 列。
Output
对于每组输入,要求输出一行,该行包含精确的 R 的 n 次方。输出需要去掉前导的 0 后不要的 0 。如果输出是整数,不要输出小数点。
Sample Input
95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201
代码如下:
#include<iostream> using namespace std;int main() { char ch[6]; int power; while(cin>>ch>>power) { int result[125]={0}; int tail=5,head=0; int after=0; while(ch[tail]=='0') tail--; while(ch[head]!='.') head++; after=tail-head; int i=0; int base=0; int p=1; int pow=power; while(tail>=0) { if(ch[tail]!='.') { result[i]=ch[tail]-'0'; base+=(ch[tail]-'0')*p; i++; p*=10; } tail--; } while(power>1) { tail=0; while(tail<i) { result[tail]=base*result[tail]; tail++; } int j=0; int before=0; int mid=0; while(j<i) { mid=(result[j]+before)%10; before=(result[j]+before)/10; result[j]=mid; j++; } while(before>0) { result[i]=before%10; before/=10; i++; } power--; } after*=pow; if(after>=i) { int zero=after-i; cout<<"."; while(zero>0) { cout<<"0"; zero--; } i--; while(i>=0) { cout<<result[i]; i--; } } else if(after<i) { i--; while(i>=0) { if(after-i==1) cout<<"."<<result[i]; else cout<<result[i]; i--; } } cout<<endl; } }
相关文章推荐
- POJ_1001(Exponentiation) 求高精度幂 攻略中...
- Exponentiation-计算精度问题
- 【原】 POJ 1001 Exponentiation 大整数乘法 解题报告
- poj 1001 Exponentiation
- HDOJ 1063 Exponentiation
- poj 1001 Exponentiation 五个小时。。。回来重写
- HDU 1063 Exponentiation
- UVaOJ 748 - Exponentiation
- hdu 1063 poj 1001 Exponentiation(幂)
- PKU-1001 Exponentiation(浮点数相乘)
- uva 748 Exponentiation(高精度实数乘法)
- UVa748 - Exponentiation
- hdu 1063 Exponentiation(大数模版题)
- POJ 1001 Exponentiation解题报告
- POJ1001 Exponentiation
- UVA 748 (暑假-高精度-C-Exponentiation)
- UVA - 748 Exponentiation
- poj 1001 Exponentiation 第一题 高精度 乘方 难度:1(非java)
- HDOJ 1063 Exponentiation
- poj 1001 Exponentiation