话说就要noip了,前两天模拟赛考了一道需要压位的高精乘高精,然后我发现高精度乘法还没有模板。话说我的写法很难看吧!因为发现考试时高精度都不是单纯的计算,又刚好学过了重载运算符,所以感觉这样写可能会方便一些吧。不管怎样,至少应该还是够用了。如果亲们看不惯就当我不存在即可。
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<cmath> #include<algorithm> using namespace std; char A[402],B[402]; struct shu{int s[1602],l;} a,b,c; void pre() { a.l=strlen(A+1); b.l=strlen(B+1); int i,t; for(i=1;i<=a.l;i++) {t=A[i]-'0'; a.s[i]=A[a.l-i+1]-'0'; a.s[a.l-i+1]=t;} for(i=1;i<=b.l;i++) {t=B[i]-'0'; b.s[i]=B[b.l-i+1]-'0'; b.s[b.l-i+1]=t;} } shu operator * (const shu &x,const shu &y) { shu ans; memset(ans.s,0,sizeof(ans.s)); int i,j; for(i=1;i<=x.l;i++) for(j=1;j<=y.l;j++) ans.s[i+j-1]+=x.s[i]*y.s[j]; ans.l=x.l+y.l-1; for(i=1;i<=ans.l;i++) {if(ans.s[i]>=10) {ans.s[i+1]+=ans.s[i]/10; ans.s[i]%=10; } } i=ans.l+1; while(ans.s[i]>0) {if(ans.s[i]>=10) {ans.s[i+1]+=ans.s[i]/10; ans.s[i]%=10; } i++; } ans.l=i-1; return ans; } int main() { while(scanf("%s%s",A+1,B+1)!=EOF) {pre(); c=a*b; for(int i=c.l;i>0;i--) printf("%d",c.s[i]); printf("\n"); } return 0; }