fork(4) download
  1. #include <iostream>
  2. #include <string>
  3. #include<algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10. std::string input1="12345678909876543212";
  11. std::string input2="12345";
  12.  
  13.  
  14. size_t n = max(input1.size(), input2.size());
  15. if (n > input1.size())
  16. input1 = string(n-input1.size(), '0') + input1;
  17. if (n > input2.size())
  18. input2 = string(n-input2.size(), '0') + input2;
  19.  
  20. string result(n+1, '0');
  21.  
  22. char carry = 0;
  23.  
  24. std::transform(input1.rbegin(),input1.rend(),input2.rbegin(),
  25. result.rbegin(),[&carry]( char x, char y){
  26. char z = (x-'0')+(y-'0') + carry;
  27. if (z > 9)
  28. {
  29. carry = 1;
  30. z -= 10;
  31. }
  32. else
  33. {
  34. carry = 0;
  35. }
  36. return z + '0';
  37. });
  38.  
  39. result[0] = carry + '0';
  40.  
  41. n = result.find_first_not_of("0");
  42. if (n != string::npos)
  43. {
  44. result = result.substr(n);
  45. }
  46.  
  47. cout << "result = " << result << endl;
  48. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
result = 12345678909876555557