In fact, the problem with big integers is the same as when we draft them. Don't tell me what you don't know ~! In fact, I didn't know its name at first;
The so-called vertical style refers to the method for you to write a draft to calculate arithmetic, elementary school knowledge; for example, you write 11 + 9:
11
+ 9
----------
20
Number A, B, sum, and difference; number length cannot exceed 1000;
Paste a code. First enter a number, which indicates the number of data groups N, and then enter N groups a B.
# Include <iostream> using namespace STD; # define Base 4 # define M 10000 char S1 [1000], S2 [1000]; int A [251], B [251], sum [251]; int max (int A, int B) {return A> B? A: B;} void trans (char * STR, int * s) // convert a string of numbers into a large INTEGER {int I, k = 1; int flag = strlen (STR)-base; for (I = flag; I> = 0; I-= base, K ++) {s [k] = STR [I]-'0'; For (Int J = I + 1; j <I + base; j ++) {s [k] = s [k] * 10 + STR [J]-'0';} I + = base; s [k] = 0; for (Int J = 0; j <I; j ++) {s [k] = s [k] * 10 + STR [J]-'0 ';} if (s [k]) s [0] = K; elses [0] = K-1;} void Prin (int * num) {printf ("% d ", num [num [0]); For (INT I = num [0]-1; I> = 1; I --) {printf ("% 04d", num [I]) ;}} void copy (int * a, int * B) // copy B to a {for (INT I = 0; I <= B [0]; I ++) A [I] = B [I];} int comp (int * a, int * B) // compare the size of a large integer. A> B returns 1, A <B returns-1, equal return 0 {if (a [0]> B [0]) return 1; if (a [0] <B [0]) Return-1; for (INT I = A [0]; I> = 1; I --) {if (a [I]> B [I]) return 1; if (A [I] <B [I]) Return-1;} return 0;} void add (int * a, int * B, int * sum) {int I, d [1000]; If (B [0] = 1) & (B [1] = 0) {copy (sum, a); retur N ;}if (A [0] = 1) & (A [1] = 0) {copy (sum, B); return ;} if (A [0]> = B [0]) {copy (sum, a); copy (D, B);} else {copy (sum, B ); copy (D, a);} // convert to sum + d (that is, the maximum and minimum values of A and B ); the final result is sum [sum [0] + 1] = 0; // store possible carry for (I = 1; I <= d [0]; I ++) // {sum [I] + = d [I] with the same processing bits; If (sum [I]> = m) {sum [I]-= m; sum [I + 1] ++ ;}}for (; I <= sum [0]; I ++) // process the Multiple Digits in A and B {If (sum [I]> = m) {sum [I]-= m; sum [I + 1] ++;} else break;} If (sum [sum [0] + 1 ]> 0) sum [0] = sum [0] + 1;} void sub (int * a, int * B, int * ans) // subtract a large integer, by default, A is greater than B {int d [1000]; int I; copy (ANS, a); copy (D, B); for (I = 1; I <= d [0]; I ++) {If (ANS [I] <D [I]) {ans [I] + = m; ans [I + 1] --;} ans [I]-= d [I];} For (; I <ans [0]; I ++) {If (ANS [I] <0) {ans [I] + = m; ans [I + 1] --;} else break;} If (! Ans [ans [0]) ans [0] --;} void mult (int * a, int * B, int * ans) {int K = A [0] + B [0]-1; // base <= 10000/* m digits * n digits = m + n-1 digits or m + n digits */For (INT I = 0; I <= K; I ++) ans [I] = 0; // Step 1: A [1], a [2]..., A [m] // B [1] // C [1], C [2],..., c [m] // Step 2: A [1], a [2]..., A [m] // B [2] // C [1], C [2],..., c [m] // Step 3 :...... for (INT I = 1; I <= B [0]; I ++) // B [] in the vertical multiplication below {int now_ I = I; if (B [I]) // when B [I] is 0, ANS [now_ I] is also 0. For (Int J = 1; j <= A [0]; j ++, now_ I ++) // A [] at the top of the multiplication vertical bar {ans [now_ I] + = A [I] * B [J]; If (ANS [now_ I]> = m) // perform the remainder carry {ans [now_ I + 1] + = ans [now_ I]/m; ans [now_ I] % = m ;}}// perform the remainder carry again, // 2 9999 9999 9999 // 2 9999 9999 // -------------- // Step 1: 9999 9999 0001 // 0002 // 9997 9999 9999 carry remainder 9999 then (+) * 9999,99970002 if (ANS [k + 1]) K ++; ans [0] = K;} int main () {int T, k = 1; CIN> T; while (t --) {cin. get (); scanf ("% S % s", S1, S2); Trans (S1, a); Trans (S2, B); add (A, B, sum); printf ("case % d: \ n", K); Prin (a); cout <"+"; Prin (B); cout <"="; Prin (SUM); cout <Endl; int FL = 1; if (COMP (a, B )! =-1) FL = 0, sub (a, B, sum); elsesub (B, A, sum); printf ("case % d: \ n", k ); prin (a); cout <"-"; Prin (B); cout <"="; if (FL) cout <"-"; Prin (SUM ); cout <Endl; mult (a, B, sum); printf ("case % d: \ n", K ++); Prin (); cout <"*"; Prin (B); cout <"="; Prin (SUM); cout <Endl; If (t) cout <Endl ;} return 0 ;}