A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 156093 Accepted Submission(s): 29551
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
Author
Ignatius.L
容易出错的地方:最后一个case的格式,输出一个换行符就行了。
#include <stdio.h>
#include <string.h>
int main(void)
{
int i,j,k,N,len_a,len_b,len_max;
scanf("%d",&N);
for(i=0;i<N;i++)
{
int a[1001]={0},b[1001]={0},c[1001]={0};
char str1[1001],str2[1001];
scanf("%s",str1);
scanf("%s",str2);
len_a=strlen(str1);
len_b=strlen(str2);
len_max=len_a>len_b?len_a:len_b;
for(j=0,k=len_a-1;j<len_a;j++,k--)
a[j]=str1[k]-'0';
for(j=0,k=len_b-1;j<len_b;j++,k--)
b[j]=str2[k]-'0';
for(j=0;j<len_max;j++)
c[j]=a[j]+b[j];
for(j=0;j<len_max;j++)
{
c[j+1]+=c[j]/10;
c[j]=c[j]%10;
}
if(c[j])
len_max++;
printf("Case %d:\n",i+1);
printf("%s + %s = ",str1,str2);
for(j=len_max-1;j>=0;j--)
printf("%d",c[j]);
if(i==N-1)
printf("\n");
else
printf("\n\n");
}
return 0;
}