Detailed topic click: http://acm.hdu.edu.cn/showproblem.php?pid=1002
Problem Descriptioni has a very simple problem for you. Given integers A and B, your job is to calculate the Sum of A + B.
Inputthe 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 both positive integers, A and B. Notice that the integers is very large, that M EANs should not process them by using 32-bit integer. Assume the length of each integer would not exceed 1000.
Outputfor Each test case, you should output of the lines. The first line was "Case #:", # means the number of the the test case. The second line is the equation "A + b = sum", sum means the result of A + B. Note there is some spaces int the Equati On. Output a blank line between the test cases.
Sample Input
21 2112233445566778899 998877665544332211
Sample Output
Case 1:1 + 2 = 3Case 2:1.,122,334,455,667,79e,+17 + 998877665544332211 = 1111111111111111110
Analysis: For this problem, there are two ways: first, the character number in the 2 string minus ' 0 ', adding a value greater than or equal to 10 can make the standard minus 10, the next self-increment 1, the subsequent processing is very simple, and the other is to read into the string after the individual characters minus ' 0 ', each corresponding into the shaping array and then Add. For the 2 methods are roughly the same, all from the back to add, every decimal, and the initialization of the array is initially 0, one side convenient operation.
#include <stdio.h> #include <stdlib.h> #include <string.h>char a[1001]; Open two character arrays A, B, as two inputs of the large number of Char B[1001];char c[1002];int main (void) {int carry = 0, N, J; int Lena, LenB, I, Lenc; scanf ("%d", &n); for (j = 1; J <= N; j + +) {memset (A, 0, 1001); memset (b, 0, 1001); memset (c, 0, 1002); scanf ("%s", a); scanf ("%s", b); Lena = strlen (a); LenB = strlen (b); For (Lena--, lenb--, i = 0, carry = 0; (Lena >= 0) && (lenb >= 0); Lena--, LenB--, i++) {c[i] = a[lena]-' 0 ' + b[lenb]-' 0 ' + carry; if ((int) c[i] > 9) {c[i] = C[i]-10 + ' 0 '; carry = 1; } else {C[i] + = ' 0 '; carry = 0; }} while (Lena >= 0) {C[i] = C[i] + A[lena] + carry;//It may be possible to add a carry and then forward the bit I F (C[i] > ' 9 ') {C[i]-= 10; carry = 1; } else carry = 0; i++; lena--; } while (LenB >= 0) {C[i] = C[i] + B[lenb] + carry; if (C[i] > ' 9 ') {C[i]-= 10; carry = 1; } else carry = 0; i++; lenb--; } Lenc = strlen (c); printf ("Case%d:\n", j); printf ("%s +%s =", A, b); for (i = lenc-1; I >= 0; i--)//c Array c[0] holds the lowest bit of the large number, and C[lenc-1] holds the highest bit of the largest number of printf ("%c", C[i]); printf ("\ n"); if (j! = N) printf ("\ n"); } return 0;}
Resources:
1, (online information) HDU 1002 A + B problem II large integer sum
HDU 1002 A + B problem II (two large numbers added)