Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <math.h>
  5. #include <cmath>
  6. #include <algorithm>
  7. using std ::cin;
  8. using std ::cout;
  9. using std ::string;
  10. int perevod (char cc) {
  11.     if (cc - '0' <= 9) {
  12.        return cc - '0';
  13.     } else {
  14.         return cc - '0' - 7;
  15.     }
  16. }
  17.  
  18. bool podh (std ::string aa, std ::string bb, std ::string cc, int pp) {
  19.     long long fir = 0;
  20.     for (int ii = aa.size() - 1; ii >= 0; --ii) {
  21.         fir += perevod(aa[ii]) * pow(pp, aa.size() - 1 - ii);
  22.     }
  23.     long long sec = 0;
  24.     for (int ii = bb.size() - 1; ii >= 0; --ii) {
  25.         sec += perevod(bb[ii]) * pow(pp, bb.size() - 1 - ii);
  26.     }
  27.     long long thrd = 0;
  28.     for (int ii = cc.size() - 1; ii >= 0; --ii) {
  29.         thrd += perevod(cc[ii]) * pow(pp, cc.size() - 1 - ii);
  30.     }
  31.     if (fir + sec == thrd) {
  32.         return 1;
  33.     } else {
  34.         return 0;
  35.     }
  36. }
  37.  
  38. int main() {
  39.     std ::string ss, aa = "", bb = "", cc = "";
  40.     std ::cin >> ss;
  41.     int minn = -1;
  42.     int pos = 1;
  43.     for (int ii = 0; ii < ss.size(); ++ii) {
  44.         if (ss[ii] != '+' && ss[ii] != '=') {
  45.             if (perevod(ss[ii]) > minn) {
  46.                 minn = perevod(ss[ii]);
  47.             }
  48.             if (pos == 1) {
  49.                 aa += ss[ii];
  50.             } else if (pos == 2) {
  51.                 bb += ss[ii];
  52.             } else {
  53.                 cc += ss[ii];
  54.             }
  55.         } else {
  56.             ++pos;
  57.         }
  58.     }
  59.     ++minn;
  60.     for (int ii = minn; ii <= 36; ++ii) {
  61.         if (podh(aa, bb, cc, ii)) {
  62.             cout << ii;
  63.             return 0;
  64.         }
  65.     }
  66.     std ::cout << -1;
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement