poj1220

高精度进制转换,java做

View Code
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
static int cal(char ch)
{
if (ch >= 'A' && ch <= 'Z')
return (int)(ch - 'A' + 10);
if (ch >= 'a' && ch <= 'z')
return (int)(ch - 'a' + 36);
return (int)(ch - '0');
}
static char get(long l)
{
if (l <= 9)
return (char)(l + '0');
if (l <= 35)
return (char)(l - 10 + 'A');
return (char)(l - 36 + 'a');
}
static public void main(String[] args) throws FileNotFoundException
{
Scanner cin = new Scanner(new BufferedInputStream(System.in));
//Scanner cin = new Scanner(new FileInputStream("t.txt"));
int t = cin.nextInt();
while (t-- != 0)
{
int a = cin.nextInt();
int b = cin.nextInt();
String st = cin.next();
BigInteger ans = new BigInteger("0");
for (int i = 0; i < st.length(); i++)
ans = ans.multiply(BigInteger.valueOf(a)).add(BigInteger.valueOf(cal(st.charAt(i))));
//System.out.println(ans);
String st1 = new String("");
while (!ans.equals(BigInteger.valueOf(0)))
{
st1 = get(ans.mod(BigInteger.valueOf(b)).longValue()) + st1;
ans = ans.divide(BigInteger.valueOf(b));
}
System.out.println(a + " " + st);
if (st1.equals(""))
System.out.println(b + " 0");
else
System.out.println(b + " " + st1);
System.out.println();
}
}
}

原文地址:https://www.cnblogs.com/rainydays/p/2183750.html