Description
A country has a budget of more than 81 trillion yen. We want to process such data, but conventional integer type which uses signed 32 bit can represent up to 2,147,483,647.
Your task is to write a program which reads two integers (more than or equal to zero) which have at most 80 digits, and prints a sum of these integers.
If given integers or the sum have more than 80 digits, print "overflow".
Input
Input consists of several datasets. In the first line, the number of datasets N is given. Each dataset consists of 2 lines:
The first integer The second integer
Output
For each dataset, print the sum of given integers in a line.
Sample Input
2 1000 800 9999999999999999999999999999999999999999 1
Output for the Sample Input
1800 10000000000000000000000000000000000000000
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { string s1, s2, temp; int sum, flag; int a, b; vector<int> v; int T; cin >> T; while (T--) { cin >> s1 >> s2; v.clear(); sum = 0; flag = 0; reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); if (s1.size() < s2.size()) { temp = s1; s1 = s2; s2 = temp; } for (int i = 0; i < s1.size(); ++i) { if (s1[i] == '0')a = 0; else if (s1[i] == '1')a = 1; else if (s1[i] == '2')a = 2; else if (s1[i] == '3')a = 3; else if (s1[i] == '4')a = 4; else if (s1[i] == '5')a = 5; else if (s1[i] == '6')a = 6; else if (s1[i] == '7')a = 7; else if (s1[i] == '8')a = 8; else if (s1[i] == '9')a = 9; if (i >= s2.size())b = 0; else { if (s2[i] == '0')b = 0; else if (s2[i] == '1')b = 1; else if (s2[i] == '2')b = 2; else if (s2[i] == '3')b = 3; else if (s2[i] == '4')b = 4; else if (s2[i] == '5')b = 5; else if (s2[i] == '6')b = 6; else if (s2[i] == '7')b = 7; else if (s2[i] == '8')b = 8; else if (s2[i] == '9')b = 9; } sum = a + b + flag; if (sum > 9) { sum = sum - 10; flag = 1; } else { flag = 0; } v.push_back(sum); } if (flag == 1) v.push_back(1); if (v.size() > 80) cout << "overflow" << endl; else { for (int i = v.size() - 1; i >= 0; --i) { cout << v[i]; } cout << endl; } } return 0; }