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 computer systems.
This problem requires that you write a program to compute the exact value of Rn 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
这道题的输入数据中的底数要考虑的情况较多,对于高精度运算中多于的 0 要进行过滤,对小数点的处理也要十分注意,具体有一下几种特殊情况:
1、 多余前置 0 : 01.111
2、 多余后置 0 : 80.000
3、 无小数点的情况: 100000
4、 特殊情况补 0 : 0.0001
以上为要注意的情况,代码如下:
Memory : 320K
Time : 16MS
//PKU1001 #include<iostream> using namespace std; void mult(char a[],char b[],char s[])//高精乘 { int i,j,k=0,alen,blen,sum=0,res[200][200]={0},flag=0; char result[200]; alen=strlen(a);blen=strlen(b); for (i=0;i<alen;i++) for (j=0;j<blen;j++) res[i][j]=(a[i]-'0')*(b[j]-'0'); for (i=alen-1;i>=0;i--) { for (j=blen-1;j>=0;j--) sum=sum+res[i+blen-j-1][j]; result[k]=sum%10; k=k+1; sum=sum/10; } for (i=blen-2;i>=0;i--) { for (j=0;j<=i;j++) sum=sum+res[i-j][j]; result[k]=sum%10; k=k+1; sum=sum/10; } if (sum!=0) {result[k]=sum;k=k+1;} for (i=0;i<k;i++) result[i]+='0'; for (i=k-1;i>=0;i--) s[i]=result[k-1-i]; s[k]='/0'; while(1) { if (strlen(s)!=strlen(a)&&s[0]=='0') strcpy(s,s+1); else break; } }//mult int main() { int i,pl,k,n,t,z,c,j;char r[6],rr[200],b[200],all[9];int re[200]; while(scanf("%s%d",all,&n)!=EOF) { k=0; memset(r,0,sizeof(r)); memset(rr,0,sizeof(rr)); memset(b,0,sizeof(b)); memset(re,0,sizeof(re)); for(i=0;i<=5;++i) { if ((all[i])=='.') pl=i; else r[k++]=all[i]; }//把底数去掉小数点,放入r中 pl=strlen(r)-pl;//计算小数位数 strcpy(rr,r); for (i=2;i<=n;++i) mult(r,rr,rr);//计算结果 i=strlen(rr)-1;z=0; while ((rr[i]=='0')&&(i>strlen(rr)-pl*n)) {z++;--i;} if (i==strlen(rr)-pl*n) z++;//计算后置0数z c=0; while(rr[c]=='0')c++;//计算前置0数c t=strlen(rr)-pl*n-c;//计算小数点位置t if (t<0) //t小于0时在小数点后补0,并输出 { printf("."); for(j=1;j<=-t;++j) printf("0"); } for (i=c;i<=strlen(rr)-1-z;++i) //输出计算结果 { if (i==t) printf("."); printf("%c",rr[i]); } printf("/n"); } return 0; }