#include <iostream> #include <string> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::string a, b; std::cin >> a; std::cin >> b; if(a.size() < b.size()) std::swap(a, b); for (int i = b.size() - 1, j = a.size() - 1; i >= 0; i--, j--) a[j] += b[i] - '0'; for (int i = a.size() - 1; i > 0; i--) { if(a[i] > '9') { int d = a[i] - '0'; a[i - 1] += d / 10; a[i] = d % 10 + '0'; } } if (a[0] > '9') { char d = (a[0] - '0') / 10 + '0'; a[0] = (a[0] - '0') % 10 + '0'; std::cout << d << a; } else std::cout << a; 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 | #include <iostream> #include <string> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::string a, b; std::cin >> a; std::cin >> b; if(a.size() < b.size()) std::swap(a, b); for (int i = b.size() - 1, j = a.size() - 1; i >= 0; i--, j--) a[j] += b[i] - '0'; for (int i = a.size() - 1; i > 0; i--) { if(a[i] > '9') { int d = a[i] - '0'; a[i - 1] += d / 10; a[i] = d % 10 + '0'; } } if (a[0] > '9') { char d = (a[0] - '0') / 10 + '0'; a[0] = (a[0] - '0') % 10 + '0'; std::cout << d << a; } else std::cout << a; return 0; } |