Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. string Add(string &s1, string &s2)
  7. {
  8. string result(max(s1.size(), s2.size()), '0');
  9. bool carry = false;
  10.  
  11. for (int i = 0; i < result.size(); i++)
  12. {
  13. int temp = carry;
  14. carry = false;
  15.  
  16. if (i < s1.size())
  17. temp += s1[s1.size() - i - 1] - '0';
  18. if (i < s2.size())
  19. temp += s2[s2.size() - i - 1] - '0';
  20. if (temp >= 10)
  21. {
  22. carry = true;
  23. temp -= 10;
  24. }
  25.  
  26. result[result.size() - i - 1] = temp + '0';
  27. }
  28.  
  29. if (carry)
  30. result.insert(result.begin(), '1');
  31. return result;
  32. }
  33.  
  34. int main(void)
  35. {
  36. ios_base::sync_with_stdio(0);
  37. cin.tie(0);
  38. string A, B;
  39. cin >> A >> B;
  40.  
  41. string result = Add(A, B);
  42. cout << result << "\n";
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement