输入输入如题面,注意对0的处理,然后Java大数真好用。
import java.math.BigInteger; import java.util.Arrays; import java.util.*; import java.io.*; public class Main{ static int valOfChar(char a){ if(a>='0'&&a<='9')return a-'0'; else if(a>='A'&&a<='Z')return a-'A'+10; else return a-'a'+36; } static char valOfInt(int a){ if(a<10)return (char)(a+'0'); else if(a<36)return (char)(a-10+'A'); else return (char)(a-36+'a'); } static int valOfSt(String a){ int ans = 0; for(int i = 0 ; i < a.length() ; ++i){ ans = ans*10+a.charAt(i)-'0'; } return ans; } public static void main(String args[]) { int T; Scanner cin = new Scanner(System.in); String in; BigInteger res; T = cin.nextInt(); BigInteger A,B; int a,b; while (T--!=0){ res = BigInteger.valueOf(0); a = cin.nextInt(); A = BigInteger.valueOf(a); b = cin.nextInt(); B = BigInteger.valueOf(b); in = cin.next(); for(int i = 0 ; i < in.length() ; ++i){ res = res.multiply(A).add(BigInteger.valueOf(valOfChar(in.charAt(i)))); } Stack<Character>deal = new Stack<Character>(); while (res.compareTo(BigInteger.ZERO)>0){ deal.push(valOfInt(valOfSt(res.mod(B).toString()))); res = res.divide(B); } System.out.println(a+" "+in); System.out.print(b+" "); if(deal.empty())System.out.print(0); while (!deal.empty())System.out.print(deal.pop()); System.out.println(' '); } } }
总之,难受。。