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 shoshould 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 shoshould 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
Code:
1 # include <stdio. h>
2 # include <string. h>
3 main ()
4 {
5 char a1 [1001] = {'\ 0'}, b1 [1001] = {' \ 0 '};
6 int a2 [1001], b2 [1001], sum [1001];
7 int n, I; // n total times, I th I
8 int la, lb, j, k, m, r;
9
10 scanf ("% d", & n );
11 I = 1;
12 while (I <= n)
13 {
14 for (j = 0; j <1001; j ++) // Initialization
15 sum [j] = 0;
16 for (j = 0; j <1001; j ++)
17 a2 [j] = 0;
18 for (j = 0; j <1001; j ++)
19 b2 [j] = 0;
20 scanf ("% s", & a1 );
21 scanf ("% s", & b1 );
22 la = strlen (a1 );
23 lb = strlen (b1 );
24 r = 1;
25 for (j = la-1; j> = 0; j --) // type conversion to reverse order the numbers
26 {
27 a2 [r] = a1 [j]-48;
28 r ++;
29}
30 r = 1;
31 for (j = lb-1; j> = 0; j --)
32 {
33 b2 [r] = b1 [j]-48;
34 r ++;
35}
36
37 if (la> lb) // Add a large number of lengths
38 k = la;
39 else
40 k = lb;
41 for (j = 1; j <= k; j ++)
42 {
43 sum [j] = sum [j] + a2 [j] + b2 [j];
44 if (sum [j]> = 10) // carry
45 {
46 sum [j] = sum [j]-10;
47 m = j + 1;
48 sum [m] ++;
49}
50}
51
52 printf ("Case % d: \ n", I); // The output result. Pay attention to the numerical output sequence.
53 for (j = la; j> 0; j --)
54 {
55 printf ("% d", a2 [j]);
56}
57 printf ("+ ");
58 for (j = lb; j> 0; j --)
59 {
60 printf ("% d", b2 [j]);
61}
62 printf ("= ");
63 if (sum [k + 1])
64 k ++;
65 for (j = k; j> 0; j --)
66 {
67 printf ("% d", sum [j]);
68}
69 printf ("\ n ");
70 if (I <n)
71 printf ("\ n ");
72
73 I ++;
74}
75}
Tips:
1. In this question, we will first read the large integer as a string, convert it to the int type by bit, and finally evaluate it by bit;
2. When reading characters multiple times, be sure to initialize the Array (so I sacrificed nearly half an hour of debugging );
3. Pay attention to the possible carry output of the highest bit;
4. Output a blank line between two test cases. -- determine the number of cases;
5. You may assume the length of each integer will not exceed 1000. -- the array definition is greater than the scope of the question to prevent overflow;
6. You can use '\ 0' to initialize a string or char array '.
By Ren qilei