POJ 1220 (两种任意进制之间的转换(大数),java的BigInteger)

https://poj.org/problem?id=1220


N进制 向 M进制 转换,

1. 先将N进制 转换成10进制;

2.将结果逐次对M取余;


一开始用BigDecimal,没办法取余;参考大神的,用BigInteger

package com.zyh.hdu;


import java.math.BigInteger;
import java.util.Scanner;

public class Poj1220 {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		
		int t = cin.nextInt();
		while(t-- != 0){
		
			int a = cin.nextInt();
			int b = cin.nextInt();
			String aa = cin.next();
			System.out.println(a+" "+aa);
			System.out.print(b+" ");
			
			 BigInteger sum = BigInteger.ZERO;
			for(int i=0; i<aa.length();i++){
				sum = sum.multiply(BigInteger.valueOf(a)).add(BigInteger.valueOf(getnum(aa.charAt(i))));
			}
			//System.out.println(sum);
			
			String bb = "";
			
			while(!sum.equals(BigInteger.ZERO)){
				bb = retchar(sum.mod(BigInteger.valueOf(b)).intValue()) + bb;
				sum = sum.divide(BigInteger.valueOf(b));
			}
			if(bb.equals("")) bb="0";
			System.out.println(bb);
			System.out.println();
		}
	}
	static int getnum(char m){
		if(m>='0' && m<='9') return m - '0';
		else if(m>='A' && m<='Z') return m-'A'+10;
		else return m-'a'+36;
	}
	static char retchar(int i){
		if(i<=9) return (char) (i + '0');
		else if(i>=10 && i<=35) return (char)(i -10+'A');
		else return (char)(i-36+'a');
	}
}


/*
 * 
 * 
 * 
Sample Input

8
62 2 abcdefghiz
10 16 1234567890123456789012345678901234567890
16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 23 333YMHOUE8JPLT7OX6K9FYCQ8A
23 49 946B9AA02MI37E3D3MMJ4G7BL2F05
49 61 1VbDkSIMJL3JjRgAdlUfcaWj
61 5 dl9MDSWqwHjDnToKcsWE1S
5 10 42104444441001414401221302402201233340311104212022133030

Sample Output

62 abcdefghiz
2 11011100000100010111110010010110011111001001100011010010001

10 1234567890123456789012345678901234567890
16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2

16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 333YMHOUE8JPLT7OX6K9FYCQ8A

35 333YMHOUE8JPLT7OX6K9FYCQ8A
23 946B9AA02MI37E3D3MMJ4G7BL2F05

23 946B9AA02MI37E3D3MMJ4G7BL2F05
49 1VbDkSIMJL3JjRgAdlUfcaWj

49 1VbDkSIMJL3JjRgAdlUfcaWj
61 dl9MDSWqwHjDnToKcsWE1S

61 dl9MDSWqwHjDnToKcsWE1S
5 42104444441001414401221302402201233340311104212022133030

5 42104444441001414401221302402201233340311104212022133030
10 1234567890123456789012345678901234567890
 
 */