B2.cpp 629 B

12345678910111213141516171819202122232425262728
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. string add(string a, string b) {
  4. reverse(a.begin(), a.end());
  5. reverse(b.begin(), b.end());
  6. int n = max(a.size(), b.size());
  7. while (a.size() < n)a.push_back('0');
  8. while (b.size() < n)b.push_back('0');
  9. string c(n, '0');
  10. int sc = 0;
  11. for (int i = 0; i < n; i++) {
  12. int temp = sc + a[i] - '0' + b[i] - '0';
  13. sc = temp / 10;
  14. c[i] = '0' + temp % 10;
  15. }
  16. if (sc)c.push_back(sc + '0');
  17. reverse(c.begin(), c.end());
  18. return c;
  19. }
  20. int main() {
  21. string a, b;
  22. cin >> a >> b;
  23. cout << add(a, b) << endl;
  24. return 0;
  25. }