POJ 1220 高精度进制转换

2019ICPC银川题目。不太会用C++,保留一下JAVA模板

题目链接

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

import java.util.Scanner;//相当于输入输出头文件
import java.util.*;
import java.math.BigDecimal;
import java.math.*;
public class Main{
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger sum , a;
sum = BigInteger.valueOf(0);
BigInteger b, c;
String s;
b = cin.nextBigInteger();
c = cin.nextBigInteger();
s = cin.next();
int len = s.length();
BigInteger tmp = BigInteger.valueOf(1);
for(int i = len - 1; i >= 0; --i ) {
int k = 0;
char cc = s.charAt(i);
if(cc >= 'A' && cc <= 'Z') {
k = cc - 'A' + 10;
}
if(cc >= '0' && cc <= '9') {
k = cc - '0';
}
if(cc >= 'a' && cc <= 'z') {
k = cc - 'a' + 36;
}
sum = sum.add(BigInteger.valueOf(k).multiply(tmp));
tmp = tmp.multiply(b);
}
int cnt = 0;
int ans[] = new int[10005];
while(sum.compareTo(BigInteger.valueOf(0))!=0) {
ans[++cnt] = sum.mod(c).intValue();
sum = sum.divide(c);
}
if(cnt == 0) {
System.out.println('0');
}
else{
char answer[] = new char[10005];
int tot = 0;
for(int i = cnt; i >= 1; --i ) {

if(ans[i] >= 0 && ans[i] <= 9) {
answer[++tot] = (char)(ans[i] + '0');
}
if(ans[i] >= 10 && ans[i] <= 35) {
char p = (char)(ans[i] - 10 + 'A');
answer[++tot] = p;
}
if(ans[i] >= 36 && ans[i] <= 61) {
char p = (char)(ans[i] - 36 + 'a');
answer[++tot] = p;
}
}
for(int i = 1; i <= tot; ++i) {
System.out.print(answer[i]);
}
}

}
}