HDU 1002 大数A+B

              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
 
代码:
 1 #include<cstring>

 2 #include<cstdio>

 3 using namespace std;

 4 

 5 int main()

 6 {

 7     //freopen("in.txt","r",stdin);

 8     int count,temp;

 9     int a1[1010],a2[1010];

10     char s1[1010],s2[1010];

11     int t,i,j,len1,len2;

12     scanf("%d",&count);

13     t=1;

14     while(count--)

15     {

16         printf("Case %d:\n",t++);

17         memset(a1,0,sizeof(a1));

18         memset(a2,0,sizeof(a2));

19         scanf("%s%s",s1,s2);

20         printf("%s + %s = ",s1,s2);

21         int len1=strlen(s1);

22         int len2=strlen(s2);

23         for(i=len1-1,j=0;i>=0;i--)

24         a1[j++]=s1[i]-'0';

25         for(i=len2-1,j=0;i>=0;i--)

26         a2[j++]=s2[i]-'0';

27         int maxlen=len1>len2?len1:len2;

28         for(i=0;i<maxlen;i++)

29         {

30             a1[i]+=a2[i];

31             if(a1[i]>=10)

32             {

33                 a1[i+1]+=1;

34                 a1[i]-=10;

35             }

36         }

37         for(j=1009;j>=0;j--)

38         if(a1[j])break;

39         if(j==-1)

40         printf("0");

41         for(;j>=0;j--)

42         printf("%d",a1[j]);

43         printf("\n");

44         if(count!=0)

45         printf("\n");

46     }

47     return 0;

48 }

 

你可能感兴趣的:(HDU)