JosepRivaille

P55982: F006A. Sabeu sumar?

Jun 2nd, 2015
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. string suma(string x, string y) {
  7.     int tam_x = x.size();
  8.     int tam_y = y.size();
  9.     string z(tam_x + 1, '0');
  10.     int carry = 0;
  11.     for (int i = 0; i < tam_x; ++i) {
  12.         int val = carry + x[tam_x - i - 1] - '0';
  13.         if (i < tam_y) val += y[tam_y - i - 1] - '0';
  14.         z[tam_x - i] = val%10 + '0';
  15.         carry = val/10;
  16.     }
  17.     z[0] += carry;
  18.     return z;
  19. }
  20.  
  21.  
  22. int main() {
  23.     string x, y;
  24.     while (cin >> x >> y) cout << suma(x,y) << endl;
  25. }
  26.  
  27. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment