Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. std::string sum(const std::string& a, const std::string& b);
  2. int main() {
  3.     std::string a;
  4.     std::cin >> a;
  5.     std::string b;
  6.     std::cin >> b;
  7.     std::cout << sum(a, b);
  8.     return 0;
  9. }
  10. std::string sum(const std::string& a, const std::string& b) {
  11.     std::string sum;
  12.     int i = a.size() - 1;
  13.     int j = b.size() - 1;
  14.     int one = 0;
  15.     while ((i >= 0) || (j >= 0)) {
  16.         char ai = (i >= 0) ? a[i] : '0';
  17.         char bj = (j >= 0) ? b[j] : '0';
  18.         int s = static_cast<int>(ai - 48) +
  19.                 static_cast<int>(bj - 48) + one;
  20.         if (s >= 10) {
  21.             sum = static_cast<char>(s - 10 + 48) + sum;
  22.             one = 1;
  23.         } else {
  24.             sum = static_cast<char>(s + 48) + sum;
  25.             one = 0;
  26.         }
  27.         --i;
  28.         --j;
  29.     }
  30.     if (one == 1)
  31.         sum = '1' + sum;
  32.     return sum;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement