hdu 1002 A + B Problem II
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
简单模拟题
#include<stdio.h>
#include<string.h>
int main()
{
int t,m=1;
scanf("%d",&t);
getchar();
while(t--)
{
char a[1399]={'\0'},b[1344]={0};
scanf("%s %s",a,b);
getchar();
int a1[1300]={0},b1[1300]={0};
int x=strlen(a)-1,y=strlen(b)-1;
int i,j=0;
for(i=x;i>=0;i--,j++)
{
a1[j]=a[i]-48;
}
i,j=0;
for(i=y;i>=0;i--,j++)
{
b1[j]=b[i]-48;
}
int f=0;
int c[1300]={0};
for(int k=0;k<=x||k<=y;k++)
{
c[k]=(a1[k]+b1[k]+f)%10;
if(a1[k]+b1[k]+f>9) f=1;
else f=0;
}
int max=x>y?x:y;
printf("Case %d:\n",m++);
printf("%s + %s = ",a,b);
if(f==1)
{
c[max+1]=1;
for(int k=max+1;k>=0;k--) printf("%d",c[k]);
}
else
{
for(int k=max;k>=0;k--) printf("%d",c[k]);
}
if(t==0) printf("\n");
else printf("\n\n");
}
return 0;
}