-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP343.cpp
55 lines (50 loc) · 1.37 KB
/
P343.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
#include <iostream>
typedef std::string String;
typedef unsigned long long ul;
int hexToInt(char c) {
if(c >= '0' && c <= '9')
return c-'0';
return 10 + c - 'A';
}
int minBase(String &s) {
int ret = 2;
for(unsigned int i = 0; i < s.size(); ++i) {
int base = hexToInt(s[i]);
if(base >= ret)
ret = base + 1;
}
return ret;
}
ul decode(String &s, int base) {
ul ret = 0;
for(unsigned int i = 0; i < s.size(); ++i)
ret = base*ret + hexToInt(s[i]);
//std::cerr << s << " base " << base << " = " << ret << std::endl;
return ret;
}
int main() {
String a, b;
ul decodedA[36], decodedB[36];
while(std::cin >> a >> b) {
int minBaseA = minBase(a);
int minBaseB = minBase(b);
for(int baseA = minBaseA; baseA <= 36; ++baseA) {
decodedA[baseA-1] = decode(a, baseA);
}
for(int baseB = minBaseB; baseB <= 36; ++baseB) {
decodedB[baseB-1] = decode(b, baseB);
}
bool printed = false;
for(int baseA = minBaseA; !printed && baseA <= 36; ++baseA) {
for(int baseB = minBaseB; !printed && baseB <= 36; ++baseB) {
if(decodedA[baseA-1] == decodedB[baseB-1]) {
std::cout << a << " (base " << baseA << ") = " << b << " (base " << baseB << ")" << std::endl;
printed = true;
}
}
}
if(!printed)
std::cout << a << " is not equal to " << b << " in any base 2..36" << std::endl;
}
return 0;
}