当前位置: 代码迷 >> 综合 >> POJ 1220 NUMBER BASE CONVERSION JAVA .
  详细解决方案

POJ 1220 NUMBER BASE CONVERSION JAVA .

热度:103   发布时间:2023-09-23 04:26:00.0
题目地址: http://poj.org/problem?id=1220


import java.util.*;
import java.math.*;
import java.text.*;
import java.io.*;public class Main
{public static int getNum(char ch){if(Character.isDigit(ch)) return ch-'0';else if(Character.isUpperCase(ch)) return ch-'A'+10;else if(Character.isLowerCase(ch)) return ch-'a'+36;return 0;}public static char getChar(int i){if(i>=0&&i<=9) return (char)(i+'0');else if(i>=10&&i<=35) return (char)(i+'A'-10);else if(i>=36) return (char)(i+'a'-36);return '0';}public static void main(String[] args) {Scanner cin = new Scanner(new BufferedInputStream(System.in));int T=cin.nextInt();while(T--!=0){int base1=cin.nextInt(),base2=cin.nextInt();String s=cin.next();BigInteger n = BigInteger.ZERO;for(int i=0;i<s.length();i++){char ch=s.charAt(i);n = n.multiply(n.valueOf(base1)).add(n.valueOf(getNum(ch)));}String ans = new String();while(!n.equals(BigInteger.ZERO)) {ans=getChar(n.mod(n.valueOf(base2)).intValue())+ans;n=n.divide(n.valueOf(base2));}if(ans.length()==0) ans+='0';System.out.println(base1+" "+s);System.out.println(base2+" "+ans);System.out.println();}}
}


  相关解决方案