[Neuqacm oj] 1018: A + B again, neuqacmoj
1018: A + B again Title Description
Gu xuecang has A very simple question for you. Here are two integers A and B. Your task is to calculate A + B.
Input
The first line of the input contains an integer T (T <= 20) indicating the number of test instances, and then 2 * T representing the positive integers A and B respectively. Note that the integer is very large, which means you cannot use a 32-bit integer for processing. You can determine that the length of an integer cannot exceed 1000.
Output
For each example, you should output two rows. The first row is "Case #:", # indicates the first few examples, the second row is an equation "A + B = Sum". Sum indicates the result of A + B. Note that there are spaces in the equation.
Sample Input
212112233445566778899998877665544332211
Sample output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
Prompt
The addition of large numbers is relatively simple in the calculation of large numbers. But for beginners, the idea is clear, but the ability to write code is obviously not enough.
Let's analyze the following:
In the previous Case, you only need to output the corresponding known data. Next we will focus on the analysis of the addition results.
First, we can see that his input is continuous input, that is, there is no space between the digits of this big number, which means it is difficult to implement data input using an integer array. Therefore, we want to solve the input problem with arrays.
For convenience, we enter the character array a as "98" and B as "9" to analyze the ideas.
The principle is to simulate the Primary School addition. To do addition, each number must be aligned before the corresponding addition can be made. Therefore, we consider defining the character array char c [1001] to transfer and align the elements in a and B;
Cin> a; "98"
The Code is as follows:
1 # include <iostream> 2 # include <cstring> 3 using namespace std; 4 5 char a [1001], B [1001]; 6 7 void Add () // character addition 8 {9 10 char c [1001]; 11 memset (c, '0', 1001); 12 for (int I = 0; I <strlen (a); I ++) // alignment 13 {14 c [I] = a [strlen (a)-1-i]; 15} 16 17 18 memset (a, '0', 1001); 19 for (int I = 0; I <strlen (B); I ++) 20 {21 a [I] = B [strlen (B)-1-i]; 22} 23 24 for (int I = 0; I <1000; I ++) 25 {26 a [I] + = c [I]-'0'; 27 if (a [I]> '9 ') 28 {29 a [I]-= 10; 30 A [I + 1] ++; 31} 32} 33 34} 35 36 37 int main () 38 {39 int T, n = 0; 40 cin> T; 41 while (T --) 42 {43 n ++; 44 memset (a, '0', 1001); 45 memset (B, '0', 1001 ); 46 47 cin> a> B; 48 49 cout <"Case" <n <":" <endl; 50 cout <a <"+" <B <"="; 51 52 53 Add (); 54 55 int k; 56 for (k = 1000; k> = 0; k --) // The leading 0 57 {58 if (a [k]! = '0') 59 {break;} 60} 61 for (int I = k; I> = 0; I --) 62 {63 cout <a [I]; 64} 65 cout <endl; 66} 67 return 0; 68}