Description
Input
Output
Sample Input
1 1 2
Sample Output
3
Hint
本题题目没明白说明有多大的数,主要是A, B < 32768迷惑人,好像不是大数,只是后面 The size of input will not exceed 50K 的这句话就说明是大数了能够为接近无穷大的负数。
事实上50K就应该开多大的数组呢?50 * 1024 / 8 == 6400,所以会有6400个数位。
这里直接使用C++的vector或者string,然后输入使用buffer,那么就能够无论数位有多大了。
大数加法比較easy,假设是减法那么题目就比較麻烦了。眼下还想不到比較简洁的解法。要特殊处理符号,并且本题是两个数都可能是负数。那么就要分开情况讨论了,符号不同,绝对值大小不同都须要不同处理,情况分的好,那么程序就会相对简洁点。
#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 ((' ' == c || ' ' == c) && len) c = getFromBuf(); while (' ' != 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(' '); } return 0; }