hdoj 1002 A+B(2)
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2 1 2 112233445566778899 998877665544332211
Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #define N 1010 5 int t; 6 char str1[N], str2[N]; 7 int sum1[N], sum2[N]; 8 int main() 9 { 10 int len1, len2, num; 11 scanf("%d", &t); 12 //getchar(); 13 num = 1; 14 while(t--) 15 { 16 memset(sum1, 0, sizeof(sum1)); 17 memset(sum2, 0, sizeof(sum2)); 18 scanf("%s", str1); 19 scanf("%s", str2); 20 len1 = strlen(str1); 21 len2 = strlen(str2); 22 int i, j; 23 for(i = len1-1, j = 0; i >= 0; i--) 24 sum1[j++] = sum1[j] + str1[i] - '0'; 25 for(i = len2-1, j = 0; i >= 0; i--) 26 sum2[j++] = sum2[j] + str2[i] - '0'; 27 for(i = 0; i < N; i++) 28 29 { 30 sum2[i] += sum1[i]; 31 if(sum2[i] >= 10) 32 { 33 sum2[i] -= 10; 34 sum2[i+1]++; 35 } 36 } 37 for(i = N-1; i>=0 && sum2[i]==0; i--) 38 ; 39 printf("Case %d:\n", num++); 40 printf("%s + %s = ", str1, str2); 41 if(i>=0) 42 { 43 for(; i >= 0; i--) 44 printf("%d", sum2[i]); 45 } 46 else 47 printf("0"); 48 if(t)//注意格式 49 printf("\n\n"); 50 else 51 printf("\n"); 52 } 53 return 0; 54 }