fork(1) download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <cstdlib>
  5.  
  6. const int base = 1000*1000*1000;
  7.  
  8. using namespace std;
  9.  
  10. vector <int> convert (string s)
  11. {
  12. vector <int> vec1;
  13. size_t i = s.length();
  14. for (; i>9; i-=9)
  15. vec1.push_back (atoi (s.substr (i-9, 9).c_str()));
  16. vec1.push_back (atoi (s.substr (0, i).c_str()));
  17. return vec1;
  18. }
  19.  
  20. int main()
  21. {
  22. string a, b;
  23. vector<int> vec1;
  24. vector<int> vec2;
  25. cin >> a >> b;
  26. vec1=convert(a);
  27. vec2=convert(b);
  28.  
  29. //for (int i=0; i<vec1.size(); i++)
  30. //cout << vec1[i] << " ";
  31. int carry = 0;
  32. for (size_t i=0; i<max(vec1.size(),vec2.size()) || carry; ++i)
  33. {
  34. if (i == vec1.size())
  35. vec1.push_back (0);
  36. vec1[i] += carry + (i < vec2.size() ? vec2[i] : 0);
  37. carry = (vec1[i] >= base) ? 1 : 0;
  38. if (carry) vec1[i] -= base;
  39. }
  40.  
  41. if (vec1.size() > 0) cout << vec1[vec1.size() - 1];
  42. for (int i=vec1.size()-2; i>=0; i--)
  43. cout << setfill('0') << setw(9) << vec1[i];
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 3468KB
stdin
999999999999
1
stdout
1000000000000