// UVa 10013 - Super long sums #include <iostream> #include <vector> using namespace std; int readNumbers(vector<int>& numberA, vector<int>& numberB) { int digitCount; cin >> digitCount; for (int i = 0; i < digitCount; i++) { int digit; cin >> digit; numberA.push_back(digit); cin >> digit; numberB.push_back(digit); } return digitCount; } int addTo(vector<int>& numberA, const vector<int>& numberB) { int addOne = 0; for (int i = numberA.size() - 1; i >= 0; i--) { int sum = numberA[i] + numberB[i] + addOne; numberA[i] = sum >= 10 ? sum - 10 : sum; addOne = sum >= 10 ? 1 : 0; } return addOne; } void printNumber(int firstDigit, const vector<int>& numberA) { if (firstDigit > 0) cout << firstDigit; for (int i = 0; i < numberA.size(); i++) { cout << numberA[i]; } cout << endl; } int main() { int cases; for (cin >> cases; cases; cases--) { vector<int> numberA, numberB; int digitCount = readNumbers(numberA, numberB); int firstDigit = addTo(numberA, numberB); printNumber(firstDigit, numberA); if (cases > 1) cout << endl; } return 0; }
Monday, November 14, 2016
UVa 10013 - Super long sums
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment