大数相加

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 int main()
 4 {
 5 
 6         int x, sa[1001], sb[1001], k = 1, i, j, m, n, t, d;
 7         char a[1001], b[1001];
 8         scanf("%d", &x);
 9         while( x-- ){
10 
11                 //int sa[1001] = {0};
12                 //int sb[1001] = {0};#1
13                 memset(sa, 0, sizeof(sa));
14                 memset(sb, 0, sizeof(sb));
15                 scanf("%s", a);
16                 scanf("%s", b);
17                 m = strlen(a);
18                 n = strlen(b);
19                 for( i = 0, j = m - 1; j >= 0; j-- )
20                 {
21 
22                         //sa[i++] = a[j];
23                         sa[i++] = a[j] - '0';
24 
25                 }
26                 for( i = 0, j = n - 1; j >= 0; j-- )
27                 {
28 
29                         //sb[i++] = b[j];
30                         sb[i++] = b[j] - '0';
31                 }
32                 t = m >= n ? m : n;/*找出最长的长度*/
33                 for( i = 0; i < t; i++ )
34                 {
35 
36                         sa[i] += sb[i];
37                         if( sa[i] >= 10 )
38                         {
39 
40                                 sa[i] %= 10;    
41                                 sa[i + 1]++;
42 
43                         }
44 
45                 }
46                 //d = (sa[i] == 1) ? t : t - abs(m - n) ;
47                 d = (sa[i] == 1) ? t : t - 1;/*如果最高位还进1位,那就从它的上位开始输出*/
48                 printf("Case %d:\n%s + %s = ", k++, a, b);
49                 while( d >= 0 ){
50 
51                         //printf("%d", d--);
52                           printf("%d", sa[d--]);    
53 
54                 }
55                 putchar('\n');
56                 //putchar('\n');细节问题:最后一次测试无空行
57                 //return 0;
58                 if( x == 0 )
59                         continue;
60                 putchar('\n');
61 
62         }
63         return 0;
64 
65 }
View Code

 

 
note:
#1只有定义变量才可以全部赋值,但只能是0。一般不喜欢这样做,所以调用头文件<string.h>里的memset函数,它不仅可以全盘赋值,而且赋值自由。
#2 细节问题:因为Output a blank line between two test cases,所以最后一组数据后面没有空行
posted @ 2013-07-29 17:11  心中的阿哲  阅读(205)  评论(0)    收藏  举报