Description
Jiajia and wind have a very cute daughter called Autumn. She is so clever that she can do integer additions when she was just 2 years old! Since a lot of people suspect that autumn may make mistakes, please write a program to prove that autumn is a real genius.
Input
The first line contains a single integer t, the number of test cases. the following lines contain 2 integers A, B (a, B <32768) each. the size of input will not exceed 50 K.
Output
The output shoshould contain t lines, each with a single integer, representing the corresponding sum.
Sample Input
11 2
Sample output
3
Hint
There may be '+ 'before the non-negative number!
The question of this question does not clearly explain how big the number is, mainly a and B <32768 confused, as if not a large number, however, the following statement of the size of input will not exceed 50 k indicates that it is a large number and can be a negative number close to infinity.
In fact, how much array should 50 k be opened? 50*1024/8 = 6400, so there will be 6400 digits.
Here, we directly use the vector or string of C ++, and then input the buffer, regardless of the number of digits.
The addition of large numbers is relatively easy. If it is a subtraction method, the problem will be more troublesome. At present, we can't think of a simple solution. We need to specially process the symbols. In this question, if both numbers may be negative, we need to separate the situations and discuss the differences between the symbols, if the absolute values are different, they must be processed and the case score is good, so the program will be relatively concise.
#include <stdio.h>#include <string>#include <algorithm>#include <vector>using namespace std;const int MAX_BUF = 512;int id = 0, len = 0;char buf[MAX_BUF];char getFromBuf(){if (id >= len){len = fread(buf, 1, MAX_BUF, stdin);id = 0;}return buf[id++];}void getNum(vector<short> &num){char c = getFromBuf();while (('\n' == c || ' ' == c) && len) c = getFromBuf();while ('\n' != c && ' ' != c && len){num.push_back(c-'0');c = getFromBuf();}}void addBigNum(vector<short> &rs, vector<short> &A, vector<short> &B){rs.clear();if (A.empty()){rs = B;return;}if (B.empty()){rs = A;return;}int an = A[0] == '+'-'0' || A[0] == '-'-'0'? 1:0;int bn = B[0] == '+'-'0' || B[0] == '-'-'0'? 1:0;short carry = 0;int i = (int)A.size()-1;int j = (int)B.size()-1;for (; i >= an || j >= bn || carry; i--, j--){short a = i >= an? A[i] : 0;short b = j >= bn? B[j] : 0;carry += a + b;rs.push_back(carry % 10);carry /= 10;}reverse(rs.begin(), rs.end());}void minusBigNum(vector<short> &rs, vector<short> &A, vector<short> &B){rs.clear();if (B.empty()){if (A.empty()) return;int i = A[0] == '-'-'0' || A[0] == '+'-'0'? 1 : 0;for (; i < (int)A.size(); i++) rs.push_back(A[i]);return ;}int an = A[0] == '-'-'0' || A[0] == '+'-'0' ? 1 : 0;int bn = B[0] == '-'-'0' || B[0] == '+'-'0' ? 1 : 0;short carry = 0;int i = (int)A.size() - 1;int j = (int)B.size() - 1;for (; i >= an || j >= bn || carry != 0; i--, j--){short a = i >= an ? A[i] : 0;short b = j >= bn ? B[j] : 0;if (a > b){short sum = a - b - carry;carry = 0;rs.push_back(sum);}else if (a < b){short sum = 10 - (b - a) - carry;carry = 1;rs.push_back(sum);}else{short sum = 0;if (carry) sum = 9;rs.push_back(sum);}}reverse(rs.begin(), rs.end());}int cmp(vector<short> &A, vector<short> &B){int an, bn;if (A.empty()) an = 0;else an = A[0] == '-'-'0' || A[0] == '+'-'0'? A.size()-1 : A.size();if (B.empty()) bn = 0;else bn = B[0] == '-'-'0' || B[0] == '+'-'0'? B.size()-1 : B.size();if (an > bn) return 1;if (an < bn) return -1;if (!an) return 0;int i = A[0] == '-'-'0' || A[0] == '+'-'0'? 1 : 0;int j = B[0] == '-'-'0' || B[0] == '+'-'0'? 1 : 0;int res = 0;for (; i < (int)A.size(); i++, j++){if (A[i] < B[j]) res = -1;else if (A[i] > B[j]) res = 1;if (res != 0) break;}return res;}int main(){int T;scanf("%d", &T);while (T--){vector<short> A, B, rs;getNum(A);getNum(B);bool Asign = true, Bsign = true;if (!A.empty() && A[0] == '-'-'0') Asign = false;if (!B.empty() && B[0] == '-'-'0') Bsign = false;if (Asign == Bsign){addBigNum(rs, A, B);if (!Asign) putchar('-');}else{int c = cmp(A, B);if (0 == c) rs.push_back(0);else if (c < 0){minusBigNum(rs, B, A);if (!Bsign) putchar('-');}else{minusBigNum(rs, A, B);if (!Asign) putchar('-');}}for (int i = 0; i < (int)rs.size(); i++){printf("%d", rs[i]);}putchar('\n');}return 0;}
Poj 2756 Autumn is a genius big number addition and subtraction