大数相加,转的算法很明显:
#include <stdio.h> #include <string.h> char a[105],b[105]; char* add(char *a,char *b) { int i,j,k = 0,tmp[105],l1 = strlen(a),l2 = strlen(b); for (i = l1-1,j = l2-1;i >= 0 && j >= 0;--i,--j) tmp[k++] = a[i]+b[j]-'0'-'0'; for (;i >= 0;--i) tmp[k++] = a[i]-'0'; for (;j >= 0;--j) tmp[k++] = b[j]-'0'; tmp[k] = 0; for (i = 0;i < k;++i) { tmp[i+1] += tmp[i]/10; tmp[i] %= 10; } if (!tmp[k]) --k; for (i = 0;i <= k;++i) a[i] = tmp[k-i] + '0'; a[k+1] = '\0'; return a; } int main() { char *r; while (scanf("%s%s",a,b) != EOF) { r = add(a,b); printf("%s\n",r); } return 0; }