-
Notifications
You must be signed in to change notification settings - Fork 2
/
allaboutthatbase.cpp
153 lines (142 loc) · 2.8 KB
/
allaboutthatbase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#define _USE_MATH_DEFINES
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ll valueConverter(char num)
{
if(num >= 'a' && num <= 'z')
{
return num - 'a' + 10;
}
else if(num == '0')
{
return 2;
}
else
{
return num - '0';
}
}
int main()
{
ll i;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll cases;
cin >> cases;
for(i = 0; i < cases; i++)
{
string num1Str, num2Str, answerStr;
char op, garb;
cin >> num1Str >> op >> num2Str >> garb >> answerStr;
bool foundOne = false;
ll minBase = 1;
for(auto subChar : num1Str)
{
minBase = max(minBase, valueConverter(subChar));
}
for(auto subChar : num2Str)
{
minBase = max(minBase, valueConverter(subChar));
}
for(auto subChar : answerStr)
{
minBase = max(minBase, valueConverter(subChar));
}
for(ll j = minBase; j <= 36; j++)
{
try{
ll num1;
ll num2;
ll answer;
if(j == 1)
{
num1 = num1Str.size();
num2 = num2Str.size();
answer = answerStr.size();
}
else
{
num1 = stoi(num1Str, nullptr, j);
num2 = stoi(num2Str, nullptr, j);
answer = stoi(answerStr, nullptr, j);
}
/*
cout << "num1: " << num1 << "\n";
cout << " num2: " << num2 << "\n";
cout << "answer: " << answer << "\n";
*/
bool valid = false;
switch(op)
{
case '+':
{
valid = num1 + num2 == answer;
break;
}
case '-':
{
valid = num1 - num2 == answer;
break;
}
case '*':
{
valid = num1 * num2 == answer;
break;
}
case '/':
{
ll temp = num1 / num2;
temp *= num2;
//check that the numbers cleanly divide, and then blindly integer divide
valid = temp == num1 && num1 / num2 == answer;
break;
}
}
if(valid)
{
foundOne = true;
if(j >= 10 && j != 36)
{
cout << char('a'+j-10);
}
else if(j == 36)
{
cout << 0;
}
else
{
cout << j;
}
}
}
catch(exception& e)
{
}
}
if(!foundOne)
{
cout << "invalid";
}
cout << "\n";
}
return 0;
}
//Start: Mon Jan 27 15:32:31 MST 2020
//Stop: Mon Jan 27 16:45:52 MST 2020
//Start: Wed Jan 29 09:18:48 MST 2020
//Stop: Wed Jan 29 09:30:44 MST 2020