题目大意:求两个大数的加法 //看到有说数组大小开到250,我改了之后还是不行 |
|
这道题允许
100 001 加起来 |
|
25分 |
仅供参考:
#include <stdio.h> #include <string.h> #define MAXLEN 1000 char a1[MAXLEN]; char a2[MAXLEN]; static int v1[MAXLEN]; static int v2[MAXLEN]; static int v3[MAXLEN]; int i,j,n,L,z; void main(void) { scanf("%d",&n); for (j=0;j<n;j++) { scanf("%s%s",a1,a2); L=strlen(a1); for (i=0;i<L;i++) v1[i]=a1[L-1-i]-""0""; L=strlen(a2); for (i=0;i<L;i++) v2[i]=a2[L-1-i]-""0""; for (i=0;i<MAXLEN;i++) v3[i]=v1[i]+v2[i]; for (i=0;i<MAXLEN;i++) { if (v3[i]>=10) { v3[i+1]+=v3[i]/10; v3[i]=v3[i]%10; } } printf("Case %d:\n", j+1); printf("%s + %s = ", a1, a2); z=0; for (i=MAXLEN-1;i>=0;i--) { if (z==0) { if (v3[i]!=0) { printf("%d",v3[i]); z=1; } } else { printf("%d",v3[i]); } } if (z==0) printf("0"); printf("\n"); } } //Sample Input //3 //0 0 //1 2 //112233445566778899 998877665544332211 // //Sample Output //Case 1: //0 + 0 = 0 //Case 2: //1 + 2 = 3 //Case 3: //112233445566778899 + 998877665544332211 = 1111111111111111110 |
由于最多进1
[code=c v3[i+1]+=v3[i]/10; //改为 v3[i+1]+=1; //更好,更直接 ][/code] |