poj1001解题报告

 

Exponentiation
Time Limit: 500MS   Memory Limit: 10000K
Total Submissions: 80517   Accepted: 19098

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.

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
题目其实并不难,但是我WA了n次,这道题主要考察的细节的处理,思路很简单,就是先将小数转化为整数,在对整数进行连续的高精度乘法,最后计算小数点的位置,将小数点插入。

这道题一开始我理解的思路有错误,也是导致我WA了n次的原因之一,我一开始理解为小数的长度都是六位,所以代码中所有要用到小数字符串的长度的地方我图省事就都用6代表了,但后来发现题目中说的是前六列是小数位,但没有说小数一定要沾满前六列,同样后面的指数也是占8,9列,但是如样例中也是有只占第9列的例子,所以我后来就将所有用到长度的地方都用strlen来计算了,这是一个要注意的地方,还有就是要注意去掉所有的小数点前面的0和小数点后面没有意义的0,呃。。。比较白痴的是,然后就是有几组测试数据很特殊没有考虑周全,这是从网上别人犯过的错误中学来的,就是10.000 2不是100.   还有就是4.0 2 应该是16而我的一开始居然是.16   ,然后经历一番周折最终还是A掉了,花了我不少时间。下面是A掉的代码,有些凌乱,因为做着题做到很晚,各种补,各种改,各种变量,呃。。领会精神吧!

代码:

语言:c++

#include<iostream> #include<cstring> using namespace std; int main() { char *result; char *multiply(char *m1,char *m2);//高精度乘法 char s[1000]; int n; while(cin>>s>>n) { int i,len,sign=0,k,len_result,len00=strlen(s); for(i=0;i<strlen(s);++i)//判断是否含有小数点,若有用k记录小数点的位置 if(s[i]=='.') { sign=1; k=i; break; } if(sign==0)//没有小数点的情况 { if(n==0)//指数为0 cout<<'1'<<endl; else if(n==1)//指数为1 cout<<s<<endl; else//指数>=2 { result=multiply(s,s); for(int j=3;j<=n;++j) result=multiply(result,s); cout<<result<<endl; } } else//有小数点的情况 { if(k==1&&s[0]=='0')//小数点前面为0的情况形如0.0001 { int count,len_q=0; char q[10000]; for(int j=2;j<len00;++j) { if(s[j]!='0') { count=j; break; } } for(int j=count;j<len00;++j)//q为小数转化成的整数 q[len_q++]=s[j]; q[len_q]='/0'; if(n==0)//指数为0 cout<<'1'<<endl; else if(n==1)//指数为1 { int qq=len00-1; while(s[qq]=='0'&&qq>=2)//去掉后面没有意义的0 --qq; for(int kk=1;kk<=qq;++kk) cout<<s[kk]; cout<<endl; } else//指数大于等于2 { int count_len,len_0; result=multiply(q,q); for(int j=3;j<=n;++j)//做高精度乘法 result=multiply(result,q); len_result=strlen(result); count_len=(len00-2)*n-len_result;//计算小数点的位置 cout<<'.'; for(int j=1;j<=count_len;++j) cout<<'0'; while(result[len_result-1]=='0'&&len_result>=0)//出去无意义的0 --len_result; for(int j=0;j<len_result;++j) cout<<result[j]; cout<<endl; } } else//有小数点但是小数点的前面有非零整数 { if(n==0)//指数为0 cout<<'1'<<endl; else if(n==1)//指数为1 { int yy=len00-1,rr; for(rr=0;rr<len00;++rr)//记录小数点的位置 if(s[rr]=='.') break; while(s[yy]=='0'&&yy>=2)//除去无意义的零 --yy; for(int rrr=0;rrr<rr;++rrr) cout<<s[rrr]; if(yy!=rr)//判断是否结果为整数,如不为整数输出小数点,和后面的小数部分 { cout<<'.'; for(int rrr=rr+1;rrr<=yy;++rrr) cout<<s[rrr]; cout<<endl; } else cout<<endl; } else//指数大于等于2 { char q1[1000],len_q1=0; for(int j=0;j<len00;++j)//用q1数组来保存转化后的整数 if(s[j]!='.') q1[len_q1++]=s[j]; q1[len_q1]='/0'; result=multiply(q1,q1);//高精度乘法 for(int j=3;j<=n;++j) result=multiply(result,q1); int k1,count1; len_result=strlen(result); for(int j=0;j<len00;++j)//记录小数点的位置,用k1来保存 if(s[j]=='.') { k1=j; break; } count1=(len00-1-k1)*n;//计算小数点的位置 for(int j=0;j<len_result-count1;++j) cout<<result[j]; long ii=len_result-count1; while(result[len_result-1]=='0'&&len_result>ii)//除去后面的无意义的0 --len_result; if(len_result!=ii) cout<<'.'; for(int j=ii;j<len_result;++j) cout<<result[j]; cout<<endl; } } } } return 0; } char *multiply(char *m1,char *m2)//以下为高精度乘法的所有函数 { int *str2int(char *str); char *int2str(int *a,int n); int check(int *a,int n); int len1=strlen(m1),len2=strlen(m2),len,*t1,*t2,*prod,i,j; len=len1+len2; t1=str2int(m1); t2=str2int(m2); prod=new int[(len+1)*sizeof(int)]; for(i=0;i<=len;++i) prod[i]=0; for(i=0;i<len1;++i) for(j=0;j<len2;++j) prod[i+j]+=t1[i]*t2[j]; len=check(prod,len); return int2str(prod,len); } int *str2int(char *str) { int len=strlen(str); int *a=new int[(len+1)*sizeof(int)]; for(int i=0;i<len;++i) a[i]=(int)str[len-1-i]-'0'; return a; } char *int2str(int *a,int n) { int len=n; char *str=new char[(len+1)*sizeof(char)]; for(int i=0;i<len;++i) str[i]=(char)a[len-1-i]+'0'; str[len]='/0'; return str; } int check(int *a,int n) { int len=n,i; while(len>1&&a[len-1]==0) --len; for(i=0;i<len;++i) if(a[i]>=10) { a[i+1]+=a[i]/10; a[i]%=10; } if(a[i]!=0) len=i+1; return len; }  

 

 

你可能感兴趣的:(qq,Integer,input,语言,each,output)