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): 150756    Accepted Submission(s): 28485


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 
 4 #define N 1005
 5 
 6 char ch[N],ch1[N];
 7 
 8 void add(char ch3[],char ch4[])
 9 {
10     int i = 0;
11     int j = 0;
12     int a[N];
13     memset(a,0,sizeof(a));
14     int k = 0;
15     int sum = 0;
16     int t = 0;
17 
18     int len_ch = strlen(ch3);
19     int len_ch1 = strlen(ch4)-1;
20 
21     for(i = len_ch-1; i>= 0; i--)
22     {
23         sum = ch3[i]+ch4[len_ch1] - 2*'0' + t;
24         a[k++] = sum %10;
25         t = sum /10;
26         len_ch1--;
27     }
28 
29     for(j = len_ch1; j >= 0; j--)
30     {
31         sum = ch4[j] +t -'0';
32         a[k++] = sum % 10;
33         t = sum /10;
34     }
35 
36     for(i = k-1; i >= 0; i--)
37         printf("%d",a[i]);
38         printf("\n");
39 }
40 
41 int main()
42 {
43     int t = 0;
44     scanf("%d",&t);
45     int w = 1;
46     while(t--)
47     {
48         scanf("%s%s",ch,ch1);
49         printf("Case %d:\n",w++);
50         printf("%s + %s = ",ch,ch1);
51         if(strlen(ch) > strlen(ch1))
52             add(ch1,ch);
53         else
54         add(ch,ch1);
55         if(t) printf("\n");
56     }
57     return 0;
58 }

 

posted @ 2013-04-03 14:43  heity  阅读(161)  评论(0编辑  收藏  举报