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
思路:这是一个大数相加问题,将两个非常大的数加起来求得到的结果,如果用传统的A+B肯定是不行的,因为要加的数位数非常大,将近1000位,这里我们用字符串倒叙相加,倒着输出即可。将每一个字符串倒着存储后,从第一位开始加,如果相加的值大于等于10,就让下一位加一,本位的结果为该值减十(特别注意最后一位相加的结果是否大于等于十)。将数组中存储的值倒着输出即可。代码如下:
#include<stdio.h>
#include<string.h>
int main()
{
int T,i,t,l1,l2,k,k1,k2,z,m=1;
int num[1010],f1[1010],f2[1010];
char a[1010],b[1010];
scanf("%d",&T);
while(T--)
{
k=0;k1=0,k2=0;
memset(num,0,sizeof(num));
memset(f1,0,sizeof(f1));
memset(f2,0,sizeof(f2));
scanf("%s %s",a,b);
l1=strlen(a);
l2=strlen(b);
for(i=l1-1;i>=0;i--)
f1[k1++]=a[i]-'0';
for(i=l2-1;i>=0;i--)
f2[k2++]=b[i]-'0';
if(k1>k2)
t=k1;
else
t=k2;
for(i=0;i<t;i++)
{
num[i]=num[i]+f1[i]+f2[i];
if(num[i]>=10)
{
num[i]=num[i]-10;
num[i+1]++;
}
}
printf("Case %d:\n",m++);
printf("%s + %s = ",a,b);
if(f1[t-1]+f2[t-1]>=10)
printf("1");
for(i=t-1;i>=0;i--)
printf("%d",num[i]);
printf("\n");
if(T)
printf("\n");
}
return 0;
}