题目地址:点击打开链接
把浮点数当成整数来乘,最后再加上小数点即可。
C++代码:
#include <cstdio> #include <cstring> const int maxnum=100000; char s1[maxnum],s2[maxnum],ans[maxnum],temps[maxnum]; void add(char s[]) { int len_min=strlen(s)>strlen(ans)?strlen(ans):strlen(s); int len_max=strlen(s)<strlen(ans)?strlen(ans):strlen(s); int i; int c=0; for(i=0;i<len_min;++i) { int a=s[i]-'0'+ans[i]-'0'+c; ans[i]=a%10+'0'; c=a/10; } for(i=len_min;i<len_max;++i) { int a=s[i]+ans[i]-'0'+c; ans[i]=a%10+'0'; c=a/10; } if(c!=0) ans[i]=c+'0'; } void multi(char s1[],int x,int n) { memset(temps,0,sizeof(temps)); int c=0; int i; int len=strlen(s1); for(i=0;i<len;++i) { int a=(s1[i]-'0')*x+c; temps[i]=a%10+'0'; c=a/10; } if(c!=0) temps[len++]=c+'0'; for(i=len-1;i>=0;--i) temps[i+n]=temps[i]; for(i=0;i<n;++i) temps[i]='0'; add(temps); } void switch_str(char s[]) { int i=0; int j=strlen(s)-1; for(;i<j;++i,--j) { char c=s[i]; *(s+i)=s[j]; *(s+j)=c; } } int main() { char num1[100]; int n; int dot; while(scanf("%s %d",num1,&n)!=EOF) { memset(ans,0,sizeof(ans)); memset(s1,0,sizeof(s1)); memset(s2,0,sizeof(s2)); int i,j; dot=0; for(i=strlen(num1)-1;i>=0;--i) { if(num1[i]=='.') break; dot++; } if(i<0) dot=0; int flag=0; for(i=0;num1[i]!=0;++i) { if(num1[i]!='.') s1[i-flag]=num1[i]; else flag=1; } s1[i]=0; switch_str(s1); strcpy(s2,s1); strcpy(ans,s1); for(j=1;j<n;++j) { memset(s1,0,sizeof(s1)); strcpy(s1,ans); memset(ans,0,sizeof(ans)); for(i=0;i<(int)strlen(s2);++i) multi(s1,s2[i]-'0',i); } for(i=maxnum-1;i>=0&&(ans[i]==0||ans[i]=='0');--i) ans[i]=0; dot=dot*n; if(strlen(ans)<=dot) { printf("."); for(i=dot-1;i>strlen(ans)-1;--i) printf("0"); for(;i>=0;--i) printf("%c",ans[i]); printf(" "); } else { for(i=strlen(ans)-1;i>dot-1;--i) printf("%c",ans[i]); flag=1; for(j=0;j<=i;++j) { if(ans[j]!='0') { flag=0; break; } } if(j>i) printf(" "); else { printf("."); for(;i>=j;--i) printf("%c",ans[i]); printf(" "); } } } return 0; }