两个很大的数相乘
#include <stdio.h> #include<string.h> const int MAXN=100000; char ch1[MAXN],ch2[MAXN]; int sum[MAXN]; int main(int argc, char *argv[]) { int i,j,Rem; char t; memset(ch1,0,sizeof(ch1)); memset(ch2,0,sizeof(ch2)); scanf("%s",ch1); scanf("%s",ch2); memset(sum,0,sizeof(sum)); int len1=strlen(ch1); int len2=strlen(ch2); for(i=0;i<len1;i++) ch1[i]-=48; for (i=0;i<len2;i++) ch2[i]-=48; for (i=0;i<len1/2;i++)//倒逆 { t=ch1[i]; ch1[i]=ch1[len1-1-i]; ch1[len1-1-i]=t; } for (i=0;i<len2/2;i++)//倒逆 { t=ch2[i]; ch2[i]=ch2[len2-1-i]; ch2[len2-1-i]=t; } int cas=0; for (i=0;i<len1;i++) { int c=0; for (j=0;j<MAXN;j++) { int s=ch1[i]*ch2[j]+c; sum[j+cas]=sum[j+cas]+s%10; c=s/10; } cas++; } int c=0; for (i=0;i<MAXN;i++) {/* Rem=sum[i]; sum[i]=(sum[i]+c)%10; c=Rem/10; r-1位+c进位的数导致r位的数进位就会错误了(19+c>20的话c得到的是1而不是2) */ sum[i]=sum[i]+c; Rem=sum[i]; sum[i]%=10; c=Rem/10; } for (i=MAXN-1;i>=0;i--) if(sum[i]) break; for (;i>=0;i--) printf("%d",sum[i]); printf("\n"); return 0; }