#include <algorithm> #include <iostream> int main() { std::string a, b; std::cin >> a >> b; std::string result; int carry = 0; while (!a.empty() || !b.empty()) { int sum = carry; if (!a.empty()) { sum += *a.rbegin() - '0'; a.pop_back(); } if (!b.empty()) { sum += *b.rbegin() - '0'; b.pop_back(); } if (sum >= 10) { carry = 1; sum -= 10; } else { carry = 0; } result.push_back(sum + '0'); } if (carry) { result.push_back('1'); } std::reverse(result.begin(), result.end()); std::cout << result; 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 | #include <algorithm> #include <iostream> int main() { std::string a, b; std::cin >> a >> b; std::string result; int carry = 0; while (!a.empty() || !b.empty()) { int sum = carry; if (!a.empty()) { sum += *a.rbegin() - '0'; a.pop_back(); } if (!b.empty()) { sum += *b.rbegin() - '0'; b.pop_back(); } if (sum >= 10) { carry = 1; sum -= 10; } else { carry = 0; } result.push_back(sum + '0'); } if (carry) { result.push_back('1'); } std::reverse(result.begin(), result.end()); std::cout << result; return 0; } |