首页 > 专题系列 > Java解POJ > POJ 1220 NUMBER BASE CONVERSION [解题报告] Java
2013
11-09

POJ 1220 NUMBER BASE CONVERSION [解题报告] Java

NUMBER BASE CONVERSION

问题描述 :

Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits:

{ 0-9,A-Z,a-z }

HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original number.

输入:

The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10, B = 11, …, Z = 35, a = 36, b = 37, …, z = 61 (0-9 have their usual meanings).

输出:

The output of the program should consist of three lines of output for each base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank.

样例输入:

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

样例输出:

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

解题代码:

import java.util.*;
import java.math.*;
public class Main {
	static int c2i(char c){
		if(c>='0'&&c<='9')
			return c-'0';
		if(c>='A'&&c<='Z')
			return c-'A'+10;
		return c-'a'+36;
	}
	static char i2c(int c){
		if(c< 10)
			return (char)(48+c);
		if(c>=10&&c< 36)
			return (char)('A'+c-10);
		return (char)('a'+c-36);
	}
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int t=in.nextInt();
		for(int k=0;k< t;k++){
			int a1=in.nextInt();
			int a2=in.nextInt();
			String s=in.next();
			System.out.println(a1+" "+s);
			System.out.print(a2+" ");
			BigInteger a=BigInteger.ZERO;
			for(int i=0;i< s.length();i++)
				a=a.multiply(BigInteger.valueOf(a1)).add(BigInteger.valueOf(c2i(s.charAt(i))));
			s="";
			while(!a.equals(BigInteger.ZERO)){
				s=i2c(a.mod(BigInteger.valueOf(a2)).intValue())+s;
				a=a.divide(BigInteger.valueOf(a2));
			}
			if(s.equals(""))
				s="0";
			System.out.println(s);
			System.out.println();
		}
	}

}

  1. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。