#include <iostream> #include <string> #include <vector> int main() { std::string a,b; std::cin >> a >> b; std::vector<char> numberA(5001, 0); std::vector<char> numberB(5001, 0); std::vector<char> result(5001, 0); int j = 0; for (int i = a.size() - 1; i >= 0; i--) { numberA[j++] = a[i] - '0'; } j = 0; for (int i = b.size() - 1; i >= 0; i--) { numberB[j++] = b[i] - '0'; } int maxSize = std::max(a.size(), b.size()); for (int i = 0; i < maxSize; ++i) { result[i] += (numberA[i] + numberB[i]); if (result[i] >=10) { result[i + 1] += (result[i] / 10); result[i] %= 10; } } std::string resultString; int index = maxSize; while (result[index] == 0) { --index; } while (index >= 0) { resultString.push_back(result[index--] + '0'); } std::cout << resultString << std::endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <iostream> #include <string> #include <vector> int main() { std::string a,b; std::cin >> a >> b; std::vector<char> numberA(5001, 0); std::vector<char> numberB(5001, 0); std::vector<char> result(5001, 0); int j = 0; for (int i = a.size() - 1; i >= 0; i--) { numberA[j++] = a[i] - '0'; } j = 0; for (int i = b.size() - 1; i >= 0; i--) { numberB[j++] = b[i] - '0'; } int maxSize = std::max(a.size(), b.size()); for (int i = 0; i < maxSize; ++i) { result[i] += (numberA[i] + numberB[i]); if (result[i] >=10) { result[i + 1] += (result[i] / 10); result[i] %= 10; } } std::string resultString; int index = maxSize; while (result[index] == 0) { --index; } while (index >= 0) { resultString.push_back(result[index--] + '0'); } std::cout << resultString << std::endl; return 0; } |