fork download
  1. #include<iostream>
  2. #include<string>
  3. #include<algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int to_num(char c){
  8. if(c >= '0' && c <= '9')
  9. return c - '0';
  10. else return 0;
  11. }
  12. char to_char(int c){return c + '0';}
  13.  
  14. int main(){
  15. string a, b;
  16.  
  17. cin >> a >> b;
  18. string result;
  19. bool pick_up = false;
  20. for(const auto& c : max(a, b)){
  21. int add = to_num(a.back()) + to_num(b.back());
  22. if(pick_up) add++;
  23. pick_up = false;
  24. result.push_back(to_char(add % 10));
  25. a.pop_back(); b.pop_back();
  26. if(add >= 10) pick_up = true;
  27. }
  28. if(pick_up)result.push_back('1');
  29. reverse(result.begin(), result.end());
  30. cout << result << endl;
  31. }
Runtime error #stdin #stdout 0s 5452KB
stdin
1 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
stdout
Standard output is empty