2019 ICPC Asia Yinchuan Regional I. Base62(高精度/BigInteger)
作者:互联网
As we already know, base64 is a common binary-to-text encoding scheme. Here we define a special series of positional systems that represent numbers using a base (a.k.a. radix) of 2 to 62. The symbols '0' -- '9' represent zero to nine, and 'A' -- 'Z' represent ten to thirty-five, and 'a' -- 'z' represent thirty-six to sixty-one. Now you need to convert some integer z in base x into base y.
Input
The input contains three integers x, y (2≤x,y≤62) and z(0≤z<x120), where the integer z is given in base x.
Output
Output the integer zz in base yy.
样例输入复制
16 2 FB
样例输出复制
11111011
简单的进制转换题目,不过需要用高精。C++的高精模版有可能会爆,所以可以用Java的大整数类来写(真香
注意判一下输入的z是0的情况。
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static int get(char c) {
if(c <= '9') return (c - '0');
else if(c <= 'Z') return 10 + (c - 'A');
else return 36 + (c - 'a');
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger base1, base2, a;
String s;
base1 = sc.nextBigInteger();
base2 = sc.nextBigInteger();
s = sc.next();
a = BigInteger.ZERO;
for(int i = 0; i < s.length(); i++) {
char now = s.charAt(i);
a = a.multiply(base1);
String xx = String.valueOf(get(now));
BigInteger x = new BigInteger(xx);
a = a.add(x);
}
if(a == BigInteger.ZERO) {
System.out.println("0");
return;
}
ArrayList<BigInteger> arr = new ArrayList<>();
while(a != BigInteger.ZERO) {
BigInteger aa = a;
aa = aa.divide(base2);
aa = aa.multiply(base2);
BigInteger now = a.subtract(aa);
arr.add(now);
a = a.divide(base2);
}
for(int i = arr.size() - 1; i >= 0; i--) {
BigInteger tmp = arr.get(i);
int now = tmp.intValue();
if(now <= 9) System.out.print(now);
else if(now <= 35) System.out.print((char)(now - 10 + 'A'));
else System.out.print((char)(now - 36 + 'a'));
}
}
}
标签:aa,represent,BigInteger,--,Regional,Base62,base,now 来源: https://www.cnblogs.com/lipoicyclic/p/14738221.html