(TOJ1192)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.

输入

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.

输出

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.

样例输入

2

1 2

112233445566778899 998877665544332211

样例输出

Case 1:

1 + 2 = 3



Case 2:

112233445566778899 + 998877665544332211 = 1111111111111111110
 1 #include<stdio.h>

 2 #include<string.h>

 3 #include<ctype.h>

 4 #include<math.h>

 5 

 6 char a[1001],b[1001];

 7 

 8 void deal(char *s1, char *s2)

 9 {

10     int len1,len2,i,t,flag;

11     char *p,*q;

12     int c[1003]={0};

13     len1=strlen(s1); len2=strlen(s2);

14     i=t=flag=0;

15     printf("%s + ",a);

16     printf("%s = ",b);

17     p=strrev(s1);

18     q=strrev(s2);

19     while(*p || *q){

20         if(*p) t+=*p-'0';

21         if(*q) t+=*q-'0';

22         t+=flag;

23         if(t>=10){t=t%10; c[i++]=t; flag=1;}

24         else {c[i++]=t; flag=0;}

25         p++; q++;

26         t=0;

27     }

28     if(flag) c[i]+=1;

29     else i--;

30 

31     for(; i>=0; i--){

32         printf("%d",c[i]);

33     }

34 }

35 

36 void solve()

37 {

38     int i,n,t;

39     i=1;

40     scanf("%d",&n);

41     t=n;

42     getchar();

43     while(n--){

44         scanf("%s",a);  scanf("%s",b);

45         printf("Case %d:\n",i);

46         deal(a,b);

47         if(i!=t) printf("\n\n");

48         else printf("\n");

49         i++;

50         memset(a,'\0',strlen(a)*sizeof(char));

51         memset(b,'\0',strlen(b)*sizeof(char));

52     }

53 }

54 

55 int main()

56 {

57     solve();

58     return 0;

59 }
 

你可能感兴趣的:(em)