C语言 · 大数乘法
1 #include<stdio.h> 2 #include<string.h> 3 char s[1000]; 4 void mult(char a[],char b[]) 5 { 6 int i,j,k=0,alen,blen,sum=0,res[500][500]={0},flag=0; 7 char result[500]; 8 alen=strlen(a);blen=strlen(b); 9 10 for (i=0;i<alen;i++) 11 for (j=0;j<blen;j++) res[i][j]=(a[i]-'0')*(b[j]-'0'); 12 13 for (i=alen-1;i>=0;i--) 14 { 15 for (j=blen-1;j>=0;j--) sum=sum+res[i+blen-j-1][j]; 16 result[k]=sum; 17 k=k+1; 18 sum=sum/10; 19 } 20 21 for (i=blen-2;i>=0;i--) 22 { 23 for (j=0;j<=i;j++) sum=sum+res[i-j][j]; 24 result[k]=sum; 25 k=k+1; 26 sum=sum/10; 27 } 28 if (sum!=0) {result[k]=sum;k=k+1;} 29 30 for (i=0;i<k;i++) result[i]+='0'; 31 for (i=k-1;i>=0;i--) s[i]=result[k-1-i]; 32 s[k]='\0'; 33 34 while(1) 35 { 36 if (strlen(s)!=strlen(a)&&s[0]=='0') 37 strcpy(s,s+1); 38 else 39 break; 40 } 41 } 42 int main(){ 43 char str1[1000],str2[1000]; 44 int t,frag,i; 45 // while(scanf("%s%s",str1,str2)!=EOF){ 46 scanf("%s%s",str1,str2); 47 mult(str1,str2); 48 frag=0; 49 t=strlen(s); 50 for(i=0;i<t;i++){ 51 if(s[i]!='0') 52 frag=1; 53 if(frag==1) 54 printf("%c",s[i]); 55 } 56 if(frag==0) 57 printf("0"); 58 // } 59 return 0; 60 }