hdoj 1002 A+B(2)

时间:2021-05-23 15:46:14
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
 
 #include <stdio.h>
#include <string.h>
#include <math.h>
#define N 1010
int t;
char str1[N], str2[N];
int sum1[N], sum2[N];
int main()
{
int len1, len2, num;
scanf("%d", &t);
//getchar();
num = ;
while(t--)
{
memset(sum1, , sizeof(sum1));
memset(sum2, , sizeof(sum2));
scanf("%s", str1);
scanf("%s", str2);
len1 = strlen(str1);
len2 = strlen(str2);
int i, j;
for(i = len1-, j = ; i >= ; i--)
sum1[j++] = sum1[j] + str1[i] - '';
for(i = len2-, j = ; i >= ; i--)
sum2[j++] = sum2[j] + str2[i] - '';
for(i = ; i < N; i++) {
sum2[i] += sum1[i];
if(sum2[i] >= )
{
sum2[i] -= ;
sum2[i+]++;
}
}
for(i = N-; i>= && sum2[i]==; i--)
;
printf("Case %d:\n", num++);
printf("%s + %s = ", str1, str2);
if(i>=)
{
for(; i >= ; i--)
printf("%d", sum2[i]);
}
else
printf("");
if(t)//注意格式
printf("\n\n");
else
printf("\n");
}
return ;
}