hdu 1002 A + B Problem II

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 371689    Accepted Submission(s): 72437


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
 
 
数字太大了   只能使用字符串来保存数字 
模拟我们小学学习加法的方法
从后面一位一位的加    大于9就进位
 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     char op1[1002],op2[1002];
 6     int c,i,j,n,m,s1[1002],s2[1002],len1,len2,count;
 7     count=1;
 8     scanf("%d",&n);
 9     m=n;
10     while(m--)
11     {
12         memset(s1,0,1002*sizeof(int));
13         memset(s2,0,1002*sizeof(int));
14         scanf("%s",op1);
15         scanf("%s",op2);
16         len1=strlen(op1);
17         len2=strlen(op2);
18         c=0;
19         for(i=len1-1;i>=0;i--)
20             s1[c++]=op1[i]-'0';
21         c=0;
22         for(i=len2-1;i>=0;i--)
23             s2[c++]=op2[i]-'0';
24         for(i=0;i<1002;i++)
25         {
26             s1[i]+=s2[i];
27             if(s1[i]>=10)
28             {
29                 s1[i]-=10;
30                 s1[i+1]++;
31             }
32         }
33         printf("Case %d:\n",count++);
34         printf("%s + %s = ",op1,op2);
35         for(i=1001;i>=0;i--)
36             if(s1[i])
37                 break;
38         for(j=i;j>=0;j--)
39             printf("%d",s1[j]);
40         printf("\n");
41         if(count!=n+1)
42             printf("\n");
43     }
44     return 0;
45 }

 

posted @ 2017-09-05 12:15  红雨520  阅读(114)  评论(0编辑  收藏  举报