POJ 1220 大数字的进制转换,偷下懒,用java

    题意为进制转换,Java的大数类就像是作弊

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

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        while(n!=0){
            int a=in.nextInt();
            int b=in.nextInt();
            String c=in.next();
            BigInteger sum=BigInteger.ZERO;
            for(int i=0;i<c.length();i++){
                sum=sum.multiply(BigInteger.valueOf(a)).add(BigInteger.valueOf(getnum(c.charAt(i))));
            }
            String cc="";
            while(!sum.equals(BigInteger.ZERO)){
                cc=setchar(sum.mod(BigInteger.valueOf(b)).intValue())+cc;
                sum=sum.divide(BigInteger.valueOf(b));
            }
            if(cc.equals("")) cc="0";
            System.out.println(a+" "+c);
            System.out.println(b+" "+cc);
            System.out.println();
            n--;
        }
    }
    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 setchar(int n){
        if(n<=9) return (char)(n+'0');
        else if(n<=35) return (char)(n-10+'A');
        else return (char)(n-36+'a');
    }
}

 

posted @ 2016-07-29 15:35  Pacify  阅读(450)  评论(2编辑  收藏  举报