#include <iostream> #include <algorithm> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::string a; std::string b; std::string c; std::cin >> a >> b; std::reverse(a.begin(), a.end()); std::reverse(b.begin(), b.end()); int dl = std::max(a.size(), b.size()); int8_t r = 0; c.reserve(dl+1); for(int i = 0; i <= dl; ++i) { int8_t aa = (i < a.size()) ? a[i]-'0' : 0; int8_t bb = (i < b.size()) ? b[i]-'0' : 0; int8_t cc = aa + bb + r; char ccc = '0' + ((cc < 10) ? cc : (cc - 10)); c.append(1, ccc); r = (cc < 10) ? 0 : 1; } if(*c.rbegin() == '0') { c.pop_back(); } std::reverse(c.begin(), c.end()); std::cout << c << '\n'; }
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 | #include <iostream> #include <algorithm> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::string a; std::string b; std::string c; std::cin >> a >> b; std::reverse(a.begin(), a.end()); std::reverse(b.begin(), b.end()); int dl = std::max(a.size(), b.size()); int8_t r = 0; c.reserve(dl+1); for(int i = 0; i <= dl; ++i) { int8_t aa = (i < a.size()) ? a[i]-'0' : 0; int8_t bb = (i < b.size()) ? b[i]-'0' : 0; int8_t cc = aa + bb + r; char ccc = '0' + ((cc < 10) ? cc : (cc - 10)); c.append(1, ccc); r = (cc < 10) ? 0 : 1; } if(*c.rbegin() == '0') { c.pop_back(); } std::reverse(c.begin(), c.end()); std::cout << c << '\n'; } |