Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int power(int x, int pow) {
- if (pow == 1) {
- return x;
- }
- if (pow == 0) {
- return 1;
- }
- else {
- int temp = power(x, pow / 2);
- if (pow % 2 != 0) {
- return (temp * temp * x);
- }
- else {
- return (temp * temp);
- }
- }
- }
- int sti(string a, int x) { // sti == string to integer
- int template_answ = 0, powr = 0;
- for (int i = a.size() - 1; i > -1; --i) {
- if (a[i] >= '0' && a[i] <= '9') {
- template_answ += (a[i] - '0') * power(x, powr);
- }
- else if (a[i] >= 'A' && a[i] <= 'Z') {
- template_answ += (a[i] - 'A' + 10) * power(x, powr);
- }
- powr++;
- }
- return template_answ;
- }
- int main() {
- std::ifstream fin("input.txt");
- std::ofstream fout("output.txt");
- string a;
- int b; // это что, "a+b"!?
- fin >> a >> b;
- int a_int = 0, foot = 1;
- while (a_int < b && foot < 36) {
- foot++;
- a_int = sti(a, foot);
- if (a_int == b) {
- bool f = false;
- for (int i = 0; i < a.size(); i++) {
- if (a[i] >= '0' && a[i] <= '9') {
- if (a[i] - '0' > foot - 1) {
- f = true;
- break;
- }
- }
- else if (a[i] >= 'A' && a[i] <= 'Z') {
- if (a[i] - 'A' + 10 > foot - 1) {
- f = true;
- break;
- }
- }
- }
- if (f == false) {
- fout << foot << endl;
- return 0;
- }
- }
- }
- fout << '0' << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment