#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LEN 10001 void add(char* str1, char* str2) { int len1 = strlen(str1), len2 = strlen(str2); int longer = len1 > len2 ? len1 : len2; int i = len1 - 1, j = len2 - 1, k = longer, carry = 0; int* result = (int*)malloc((longer + 1) * sizeof(int)); while (i >= 0 && j >= 0) { int sum = str1[i--] - '0' + str2[j--] - '0' + carry; result[k--] = sum % 10; carry = sum / 10; // printf("when i >= 0 j >= 0, i is %d, j is %d, k is %d,sum = %d, result = %d, carry is %d\n", i+1, j+1, k+1,sum, result[k+1], carry); } while (i >= 0) { int sum = str1[i--] - '0' + carry; result[k--] = sum % 10; carry = sum / 10; // printf("when i >= 0,i is %d, k is %d,sum = %d, result = %d, carry is %d\n", i+1,k+1,sum, result[k+1], carry); } while (j >= 0) { int sum = str2[j--] - '0' + carry; result[k--] = sum % 10; carry = sum / 10; // printf("when j >= 0,j is %d, k is %d,sum = %d, result = %d, carry is %d\n", j+1,k+1,sum, result[k+1], carry); } if(carry > 0) { result[k] = carry; } else { k++; } for(i = k; i <= longer; i++) { printf("%d",result[i]); } printf("\n"); free(result); } int main() { char str1[MAX_LEN], str2[MAX_LEN]; while(scanf("%s\n%s", str1, str2) != EOF) { add(str1, str2); } return 0; }