egogoboy

Уравнение по основанию

Jul 6th, 2022
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int power(int x, int pow) {
  6.  
  7.     if (pow == 1) {
  8.         return x;
  9.     }
  10.     if (pow == 0) {
  11.         return 1;
  12.     }
  13.     else {
  14.         int temp = power(x, pow / 2);
  15.         if (pow % 2 != 0) {
  16.             return (temp * temp * x);
  17.         }
  18.         else {
  19.             return (temp * temp);
  20.         }
  21.     }
  22.  
  23. }
  24.  
  25. int sti(string a, int x) {   // sti == string to integer
  26.  
  27.     int template_answ = 0, powr = 0;
  28.  
  29.     for (int i = a.size() - 1; i > -1; --i) {
  30.  
  31.         if (a[i] >= '0' && a[i] <= '9') {
  32.  
  33.             template_answ += (a[i] - '0') * power(x, powr);
  34.  
  35.         }
  36.         else if (a[i] >= 'A' && a[i] <= 'Z') {
  37.  
  38.             template_answ += (a[i] - 'A' + 10) * power(x, powr);
  39.  
  40.         }
  41.  
  42.         powr++;
  43.  
  44.     }
  45.  
  46.     return template_answ;
  47.  
  48. }
  49.  
  50. int main() {
  51.  
  52.     std::ifstream fin("input.txt");
  53.     std::ofstream fout("output.txt");
  54.  
  55.     string a;
  56.     int b;          // это что, "a+b"!?
  57.     fin >> a >> b;
  58.  
  59.     int a_int = 0, foot = 1;
  60.     while (a_int < b && foot < 36) {
  61.  
  62.         foot++;
  63.         a_int = sti(a, foot);
  64.  
  65.         if (a_int == b) {
  66.             bool f = false;
  67.             for (int i = 0; i < a.size(); i++) {
  68.                 if (a[i] >= '0' && a[i] <= '9') {
  69.                     if (a[i] - '0' > foot - 1) {
  70.                         f = true;
  71.                         break;
  72.                     }
  73.                 }
  74.                 else if (a[i] >= 'A' && a[i] <= 'Z') {
  75.                         if (a[i] - 'A' + 10 > foot - 1) {
  76.                             f = true;
  77.                             break;
  78.                         }
  79.                 }
  80.             }
  81.             if (f == false) {
  82.                 fout << foot << endl;
  83.                 return 0;
  84.             }
  85.         }
  86.     }
  87.  
  88.     fout << '0' << endl;
  89.     return 0;
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment