Exponentiation
Time Limit: 500MS | Memory Limit: 10000K | |
Total Submissions: 140739 | Accepted: 34388 |
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.
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.1075960194566517745610440100011.126825030131969720661201
这道题花费我快一天的时间终于AC了,在此栽倒的原因是自己太不专心和细心,在做大数乘法时漏掉了一个清零的细节,悲催啊!
解题思路:主线就是大数相乘,注意输出格式。首先对输入的数进行去小数点。然后进行大数相乘。最后对输出的处理。
代码:
#include <iostream> #include <string> #include <cstring> using namespace std; int f[100000]; int s2[100000],a[100000]; int s3[100000]; int main() { string s1; int n; while(cin>>s1>>n) { int j=0,i; int k=-1; memset(f,0,sizeof(f)); memset(s2,0,sizeof(s2)); memset(s3,0,sizeof(s3)); for(i=0;i<s1.length();i++) { if(s1[i]!='.') { a[j]=s1[i]-'0'; j++; } else k=i; } int cnt=0; if(k!=-1) cnt=s1.length()-k-1;//记录小数点的位数 int len2=j; int len3=j; for(i=0;i<len2;i++) { s3[len2-i-1]=s2[len2-i-1]=a[i]; } //大数相乘部分 for(int t=1;t<n;t++) { int i1,j1; for(i1=0;i1<len2;i1++) for(j1=0;j1<len3;j1++) { f[i1+j1]+=(s2[i1])*(s3[j1]); } int k1; //把每一位都化为个位数 for(k1=0;k1<len2+len3-1;k1++) { if(f[k1]>=10) { f[k1+1]+=f[k1]/10; f[k1]%=10; } } while(f[k1]!=0) { f[k1+1]=f[k1]/10; f[k1]%=10; k1++; } //把结果赋给数组s3 for(int p=0;p<k1;p++) { s3[p]=f[p]; } len3=k1; memset(f,0,sizeof(f));//一定记得对中间求积数组的清零 } cnt*=n;//得到最后结果的小数点后的位数 int len=len3; if(cnt==0)//若输入的数为整数直接输出 { for(int x=len-1;x>=0;x--) cout<<s3[x]; } else//对小数的输出的处理 { while(!s3[len-1]) len--;//先除去前导0 int y=0; if(cnt<len)//判断此时结果数组的长度与小数点后的位数,若小数部分的位数少于数组长度 { int k2; while(!s3[y])//除去小数部分末尾的0 { y++; if(cnt==y) break; } if(cnt==y)//若小数部分全是0,则直接把小数点以前的数输出 { for(k2=len-1;k2>=y;k2--) { cout<<s3[k2]; } } else//若小数部分不全是0 { for(k2=len-1;k2>=y;k2--) { cout<<s3[k2]; if(cnt==k2)//若k2=cnt输出‘.’ cout<<'.'; } } } else//当结果数组的长度不大于小数部分位数时 { cout<<".";//题目要求当小数小于1时输出格式为.xxxxxxxxxxx for(int m=cnt-1;m>=y;m--) cout<<s3[m]; } } cout<<endl; } return 0; }