-
Notifications
You must be signed in to change notification settings - Fork 4
/
UVA00389.cpp
46 lines (43 loc) · 1004 Bytes
/
UVA00389.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
#include <iostream>
#include <stack>
using namespace std;
int ctoi(char c) {
if (c - 'A' >= 0) return c - 'A' + 10;
else return c - '0';
}
char itoc(int i) {
if (i < 10) return i + '0';
else return i + 'A' - 10;
}
int main() {
string n;
int b1, b2;
while (cin >> n >> b1 >> b2) {
int val = 0;
for (int i = 0; i < n.length(); i++) {
val *= b1;
val += ctoi(n[i]);
}
stack<char> s;
while (val) {
s.push(itoc(val%b2));
val /= b2;
}
if (s.size() > 7) printf(" ERROR\n");
else {
for (int i = 6; i >= s.size(); i--) {
printf(" ");
if (i == 1 && s.size() == 0) {
printf("0");
break;
}
}
while (!s.empty()) {
printf("%c", s.top());
s.pop();
}
printf("\n");
}
}
return 0;
}