A + B Problem II---hdu1002
9805 ワード
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 260647 Accepted Submission(s): 50397
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<stdio.h>
2 #include<string.h>
3 #define as 1000
4 int main()
5 {
6 int as1[as+20],as2[as+20],cot=0,j,t;
7 char shuru1[as+20],shuru2[as+20];
8 int n,i;
9 scanf("%d",&n);
10 getchar();//
11 t=n;
12 while(n--)
13 {
14 cot++;
15 scanf("%s",shuru1);
16 scanf("%s",shuru2);
17 memset(as1,0,sizeof(as1));
18 memset(as2,0,sizeof(as2));
19 for(i=0,j=strlen(shuru1)-1;j>=0;j--,i++)
20 {
21 as1[i]=shuru1[j]-'0';
22
23 }
24 for(i=0,j=strlen(shuru2)-1;j>=0;j--,i++)
25 {
26 as2[i]=shuru2[j]-'0';
27 }
28 for(i=0;i<as+20;i++)
29 {
30 as1[i]+=as2[i];
31 if(as1[i]>=10)//
32 {
33 as1[i+1]++;
34 as1[i]-=10;
35 }
36
37 }
38 for(i=as+19;(i>=0)&&(as1[i]==0);i--);// 0
39 printf("Case %d:
",cot);
40 printf("%s + %s = ",shuru1,shuru2);
41 if(i>=0)
42 {
43 for(;i>=0;i--)
44 printf("%d",as1[i]);
45 }
46 else
47 printf("0");
48 printf("
");
49 if(cot!=t)
50 printf("
");//
51 }
52 return 0;
53 }