Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
所有数据都测试过,就是WA!!!!晕——好几个小时了#include<iostream.h> #include<string.h> //---------- char* Mul(char* s1,char* s2)//标准乘 {int m=strlen(s1),n=strlen(s2),i,j,k,l,Max=m>n?m:n,p=0,q=0; bool flag=0; char* ans=new char[2*Max+1]; for(i=0;i<2*Max+1;++i) ans[i]=0; //去小数点 for(i=0;i<m;i++) if('0'==s1[0]) {for(j=i;j<m-2;j++) {s1[j]=s1[j+2]; p++; } m-=2;flag=1; break; } else if('.'==s1[i]) {for(j=i;j<m-1;j++) {s1[j]=s1[j+1]; p++; } m--;flag=1; break; } for(i=0;i<n;i++) if('0'==s2[0]) {for(j=i;j<n-2;j++) {s2[j]=s2[j+2]; q++; } n-=2;flag=1; break; } else if('.'==s2[i]) {for(j=i;j<n-1;j++) {s2[j]=s2[j+1]; q++; } n--;flag=1; break; } //减'0' for(i=0;i<m;i++) s1[i]-=48; for(i=0;i<n;i++) s2[i]-=48; //做乘法 for(i=n-1;i>=0;i--) { k=n-1-i; for(j=m-1;j>=0;j--) { ans[k]+=s2[i]*s1[j]; int r=k++; do{ int t=ans[r]; ans[r]=t%10; ans[++r]+=t/10; }while(ans[r]>10); } } //加'0' for(i=0;i<2*Max+1;++i) ans[i]+='0'; //逆置 i=2*Max; while('0'==ans[i--]); i++; int a=0,b=i; while(a<=i/2) {int t=ans[a]; ans[a]=ans[b]; ans[b]=t; a++;b--; } //加小数点 if(p+q) {if(p+q>i) {int kk=p+q-i; for(j=p+q;j>=kk;j--) ans[j]=ans[j-kk]; for(j=1;j<kk;++j) ans[j]='0'; ans[0]='.'; i+=kk; } else {for(l=i;l>i-(p+q);l--) ans[l+1]=ans[l]; ans[i+1-(p+q)]='.'; i++; } } if(flag) { i=2*Max; while('0'==ans[i--]); i++; } //返回值 ans[++i]=0;return ans; } //---------- char* F(int n,char* s)//分治的递归函数 { if(1==n)return s; if(2==n) {char* s1=new char[strlen(s)+1]; strcpy(s1,s); return Mul(s,s1); } if(!(n%2)) {char* t1=F(n/2,s),*t2=new char[strlen(t1)+1]; strcpy(t2,t1); return Mul(t1,t2); } else {char *t=new char[strlen(s)+1]; strcpy(t,s);//记录s char *t1=F(n/2,t),*t2=new char[strlen(t1)+1]; strcpy(t2,t1); char* t3=Mul(t1,t2); return Mul(t3,s); } } //--------- void main() {char s[6]; int n; while(cin>>s>>n) cout<<F(n,s)<<endl; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator