1C - A + B Problem II

时间:2023-03-10 01:22:56
1C - A + B Problem II
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 // too young and didn't notice that
// "you should not process them by using 32-bit integer"
// and "Output a blank line between two test cases".
 #include<stdio.h>
int main()
{
int a, b, t, i;
scanf("%d", &t);
for(i=;i<=t;i++)
{
scanf("%d %d", &a, &b);
printf("Case %d:\n", i);
printf("%d + %d = %d\n", a, b, a+b);
printf("\n");
}
return ;
}

Wrong Answer

// long int is also 32-bit integer.
代码省略
// 不用判断哪个数较长,只要记录答案的长度. 三个数组都要初始化为0!!!
 #include<stdio.h>
#include<string.h> int reverse_add(int *a, int *b, int *c, int al, int bl)
{
int i, sum=, k, max;
if(al<bl) max=bl;
else max=al;
k=;
for(i=; i<max; i++)
{
*(c+i)=(*(a+i)+*(b+i)+k)%;
k=(*(a+i)+*(b+i)+k)/;
}
if(k!=)
{
*(c+i)=;
return max+;
}
else return max;
} int main()
{
char s1[], s2[];
int t, k, i, length_a, length_b, rmax;
scanf("%d", &t);
for(k=;k<=t;k++)
{
int a[]={}, b[]={}, c[]={}; /* 三个数组都要初始化 */
scanf("%s %s", s1, s2);
length_a=strlen(s1); length_b=strlen(s2);
for(i=; i<length_a; i++) a[i]=s1[length_a--i]-'';
for(i=; i<length_b; i++) b[i]=s2[length_b--i]-'';
rmax=reverse_add(a, b, c, length_a, length_b);
printf("Case %d:\n", k);
printf("%s + %s = ", s1, s2);
for(i=; i<rmax; i++) printf("%d", c[rmax--i]);
printf("\n");
if(t>&&k<t) printf("\n");
}
return ;
}

AC