In this problem, you will be concerned with integers with very large numbers of digits. You must write code which will repeatedly accept (until end of file) two lines each containing an unsigned integer, and output the product of the two input unsigned integers. The output must not contain any leading zeros.
You can assume that each integer will contain at most 80 digits. The input ends with an end of file.
Sample Input 0342 1298 12 3 Sample Output 443916 36 问题解析:这道题要注意几点: 1.数前面的0要去掉,要考虑到都是0的情况 2.数组的初始化,或者是清空方法 3.做乘法的步骤 code: #include<stdio.h> #include<string.h> #define INF 200 char a[INF],b[INF]; char e[INF],f[INF]; int c[INF],d[INF]; int sum[INF]; void mul(int a[200],int b[200],int sum[200]) { register int i,j; int carry; for(i=1;i<=a[0];i++) { for(j=1;j<=b[0];j++) { sum[i+j-1]+=a[i]*b[j]; } for(j=i,carry=0;j<=b[0]+i;j++) { sum[j]+=carry; carry=sum[j]/10; if(sum[j]>=10) { sum[j]%=10; } } } sum[a[0]+b[0]]==0?sum[0]=a[0]+b[0]-1:sum[0]=a[0]+b[0]; } void init(int d1[200],char a[200]) { int i,ch,len; len=strlen(a); for(i=0;i<len/2;i++) { ch=a[i]; a[i]=a[len-1-i]; a[len-1-i]=ch; } for(i=0;i<len;i++) d1[i+1]=a[i]-'0'; d1[0]=len; } void printa(int a[200]) {int i; for(i=a[0];i>0;i--) printf("%d",a[i]); printf("/n");} int main() { register int i,j,k; int m,n; int len,len1,len2; while(scanf("%s%s",a,b)!=EOF) { memset(c,0,sizeof(c)); memset(d,0,sizeof(d)); memset(sum,0,sizeof(sum)); i=0;j=0; m=n=0; len=0;len1=0;len2=0; len1=strlen(a); len2=strlen(b); while(i<len1) { if(a[i]!='0')break; else i++; } if(i==len1){printf("0/n");continue;} for(k=i;k<len1;k++) { e[m++]=a[k]; } while(j<len2) { if(b[j]!='0')break; else j++; } if(j==len2){printf("0/n");continue;} for(k=j;k<len2;k++) {f[n++]=b[k];} init(c,e); init(d,f); mul(c,d,sum); printa(sum); memset(a,'/0',sizeof(a)); //特别注意/0 memset(b,'/0',sizeof(b)); memset(e,'/0',sizeof(e)); memset(f,'/0',sizeof(f)); } return 0; } //注意这道题最多只有80位。属于大数中的小数 |