Advertisement
StabCrab

Untitled

Mar 8th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6. string sum(string a, string b)
  7. {
  8.     if (a.length() > b.length()) // вот это место тоже не эффективно, сейчас думаю как сделать нормально
  9.     {
  10.         for (int i = b.length(); i < a.length(); i++)
  11.             b += '0';
  12.     }
  13.     else
  14.     {
  15.         for (int i = a.length(); i < b.length(); i++)
  16.             a += '0';
  17.     }
  18.     bool if10 = false;
  19.     for (int i = 0; i < min(a.length(), b.length()); i++)
  20.     {
  21.         if (isalpha(a[i]) ||(isalpha(b[i])))
  22.         {
  23.             return "RORRE";
  24.         }
  25.         if(if10)
  26.         {
  27.             a[i] += b[i] - 48 + 1;
  28.             if10 = false;
  29.         }
  30.         else
  31.             a[i] += b[i] - 48;
  32.         if (a[i] > 57)
  33.         {
  34.             a[i] -= 10;
  35.             if10 = true;
  36.         }
  37.     }
  38.     if(if10)
  39.         a += '1';
  40.     return a;
  41. }
  42. int main()
  43. {
  44.     string a, b;
  45.     cin >> a;
  46.     cin >> b;
  47.     std:: reverse(a.begin(), a.end());
  48.     std:: reverse(b.begin(), b.end());
  49.     a = sum(a, b);
  50.     std:: reverse(a.begin(), a.end());
  51.     cout << a;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement