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 computer systems.
This problem requires that you write a program to compute the exact value of R n where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
This problem requires that you write a program to compute the exact value of R n where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.
Input
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.
Output
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.
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
Hint
If you don't know how to determine wheather encounted the end of input:
s is a string and n is an integer
s is a string and n is an integer
C++ while(cin>>s>>n) { ... } c while(scanf("%s%d",s,&n)==2) //to see if the scanf read in as many items as you want /*while(scanf(%s%d",s,&n)!=EOF) //this also work */ { ... }
数据输入非常规格,底数严格就6位,指数是1-20之间的整数,我们先将底数化为整数,同时记下小数的位数,进行幂运算,最后结果再处理小数的位数。
在将小数化为整数的时候要逆向存储,multi函数可以当做模板来计算整数相乘。
#include <iostream> #include <string.h> #include <string> using namespace std; int n,a[201]={0},b[201]={0},lena,lenb; //n是指数,a和b数组是用来存储底数的,lena是a的长度,lenb是b的长度 void multi() { int i,j,k,c[201]; //数组c是用来过渡的 for(k=1;k<=n-1;k++) //一共进行n-1次乘法 { memset(c,0,sizeof(c)); for(i=0;i<lena;i++) for(j=0;j<lenb;j++) { c[i+j]=c[i+j]+a[i]*b[j]; c[i+j+1]=c[i+j+1]+c[i+j]/10; c[i+j]=c[i+j]%10; } for(i=0;i<=200;i++) //将中间结果赋给数组a,即a是用来保存最终结果的 a[i]=c[i]; lena=lena+lenb; } } int main() { string R; while(cin>>R>>n) //输入数据 { int len=6; //底数的长度一定是6 int dot=-1,i,j,up=0,down=-1; //dot是小数点的位置,up是最终结果的上界,down是下界 memset(a,0,sizeof(a)); //每次都要归0处理 memset(b,0,sizeof(b)); for(i=5;R[i]=='0';i--) //将底数数值后面无关紧要的0去掉 len--; for(i=len-1,j=0;i>=0;i--,j++) //将底数逆序存储在数组a和b中,同时保存小数点所在的位置 { if(R[i]!='.') {a[j]=R[i]-'0';b[j]=R[i]-'0';} else {dot=i;j--;} } if(dot==-1) dot=0; //dot=-1说明底数是个整数 else dot=len-dot-1; //否则计算小数的位数 lena=lenb=len; multi(); //调用函数 for(i=200;a[i]==0;i--) //计算上界 up=i; up--; for(i=0;a[i]==0;i++) //计算下界 down=i; down++; if(dot==0) //没有小数,即结果是整数的话就直接输出 { for(i=up;i>=0;i--) cout<<a[i]; cout<<endl; } else { dot=dot*n-1; //计算总小数的位数 i=up; if(up<dot)i=dot; //小数位数很大,举例0.00000123 j=down; if(dot<down)j=dot;//小数位数很小,举例123000 for(;i>=j;i--) //逆序输出 { if(i==dot)cout<<"."; cout<<a[i]; } cout<<endl; } } return 0; }