forked from zhuli19901106/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOJ1220(AC).cpp
103 lines (95 loc) · 1.51 KB
/
POJ1220(AC).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
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int b1, b2;
const int MAXLEN = 1000;
char s[2000];
int a[MAXLEN];
void fix_format(int a[], int n, int b)
{
int i;
if(b <= 0){
return;
}
for(i = 0; i < n - 1; ++i){
a[i + 1] += a[i] / b;
a[i] %= b;
}
a[n - 1] %= b;
}
int val(const char ch)
{
if(ch >= '0' && ch <= '9'){
return ch - '0';
}else if(ch >= 'A' && ch <= 'Z'){
return ch - 'A' + 10;
}else if(ch >= 'a' && ch <= 'z'){
return ch - 'a' + 36;
}else{
return -1;
}
}
char digit(const int i)
{
if(i >= 0 && i <= 9){
return i + '0';
}else if(i >= 10 && i <= 35){
return i - 10 + 'A';
}else if(i >= 36 && i <= 61){
return i - 36 + 'a';
}else{
return 0;
}
}
int main()
{
int ti, t;
int i, j, v;
char ch;
while(scanf("%d", &t) == 1){
for(ti = 0; ti < t; ++ti){
memset(a, 0, MAXLEN * sizeof(int));
scanf("%d%d%s", &b1, &b2, s);
for(i = 0; s[i]; ++i){
j = MAXLEN - 1;
while(j >= 0 && !a[j]){
--j;
}
if(j >= 0){
while(j >= 0){
a[j] *= b1;
--j;
}
fix_format(a, MAXLEN, b2);
}
v = val(s[i]);
if(v > 0){
a[0] += v;
fix_format(a, MAXLEN, b2);
}
}
printf("%d %s\n", b1, s);
printf("%d ", b2);
j = MAXLEN - 1;
while(j >= 0 && !a[j]){
--j;
}
if(j < 0){
printf("0\n");
}else{
while(j >= 0){
ch = digit(a[j]);
if(ch){
printf("%c", ch);
}
--j;
}
printf("\n");
}
printf("\n");
}
}
return 0;
}