#include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(0); std::string a, b; std::cin >> a >> b; if(b.size() > a.size()) std::swap(a,b); auto ai = a.rbegin(); auto bi = b.rbegin(); int carry = 0; while(ai != a.rend()) { auto ax = *ai - '0'; auto bx = 0; if(bi != b.rend()) { bx = *bi - '0'; } auto sum = carry + ax + bx; carry = sum >= 10 ? 1 : 0; *ai = sum % 10 + '0'; ai++; if(bi != b.rend()) { bi++; } } if(carry) { std::cout << "1"; } std::cout << a << "\n"; 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 | #include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(0); std::string a, b; std::cin >> a >> b; if(b.size() > a.size()) std::swap(a,b); auto ai = a.rbegin(); auto bi = b.rbegin(); int carry = 0; while(ai != a.rend()) { auto ax = *ai - '0'; auto bx = 0; if(bi != b.rend()) { bx = *bi - '0'; } auto sum = carry + ax + bx; carry = sum >= 10 ? 1 : 0; *ai = sum % 10 + '0'; ai++; if(bi != b.rend()) { bi++; } } if(carry) { std::cout << "1"; } std::cout << a << "\n"; return 0; } |