#include <iostream> #include <string> #include <algorithm> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::string a, b; std::string product; product.reserve(std::max(a.size(), b.size()) + 1); std::cin >> a >> b; auto getDigit = [](const std::string& s, int index) { if (index < 0) { return 0; } return s[index] - '0'; }; int overflow = 0; for (int i = 0; i < a.size() || i < b.size() || overflow != 0; i++) { const int digitA = getDigit(a, (int)a.size() - 1 - i); const int digitB = getDigit(b, (int)b.size() - 1 - i); const int currentDigitAndOverflow = overflow + digitA + digitB; const int currentDigit = currentDigitAndOverflow % 10; product.push_back('0' + currentDigit); overflow = (currentDigitAndOverflow - currentDigit) / 10; } std::reverse(product.begin(), product.end()); std::cout << product; 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 41 42 43 44 45 | #include <iostream> #include <string> #include <algorithm> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::string a, b; std::string product; product.reserve(std::max(a.size(), b.size()) + 1); std::cin >> a >> b; auto getDigit = [](const std::string& s, int index) { if (index < 0) { return 0; } return s[index] - '0'; }; int overflow = 0; for (int i = 0; i < a.size() || i < b.size() || overflow != 0; i++) { const int digitA = getDigit(a, (int)a.size() - 1 - i); const int digitB = getDigit(b, (int)b.size() - 1 - i); const int currentDigitAndOverflow = overflow + digitA + digitB; const int currentDigit = currentDigitAndOverflow % 10; product.push_back('0' + currentDigit); overflow = (currentDigitAndOverflow - currentDigit) / 10; } std::reverse(product.begin(), product.end()); std::cout << product; return 0; } |