HDOJ 1335 Basically Speaking

题目链接:点击打开链接

题目大意:将一个指定进制的字符串 转换成指定进制的字符串,输出占7位

解题思路:先将该字符串转换为10进制表示的字符串,然后把十进制表示的字符串,转换成其他进制表示的字符串

java可调用Integer.parseInt(String s,int i)将字符串s以参数i为基数解析为有符号整数

Intenger.toString(int i,int j)将返回指定 j 为基数表示 i 的字符串表示形式

Basically Speaking

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3307    Accepted Submission(s): 1327


Problem Description
The Really Neato Calculator Company, Inc. has recently hired your team to help design their Super Neato Model I calculator. As a computer scientist you suggested to the company that it would be neato if this new calculator could convert among number bases. The company thought this was a stupendous idea and has asked your team to come up with the prototype program for doing base conversion. The project manager of the Super Neato Model I calculator has informed you that the calculator will have the following neato features:
It will have a 7-digit display.

Its buttons will include the capital letters A through F in addition to the digits 0 through 9.

It will support bases 2 through 16.
 

Input
The input for your prototype program will consist of one base conversion per line. There will be three numbers per line. The first number will be the number in the base you are converting from. The second number is the base you are converting from. The third number is the base you are converting to. There will be one or more blanks surrounding (on either side of) the numbers. There are several lines of input and your program should continue to read until the end of file is reached.
 

Output
The output will only be the converted number as it would appear on the display of the calculator. The number should be right justified in the 7-digit display. If the number is to large to appear on the display, then print "ERROR'' (without the quotes) right justified in the display.
 

Sample Input
 
   
1111000 2 10 1111000 2 16 2102101 3 10 2102101 3 15 12312 4 2 1A 15 2 1234567 10 16 ABCD 16 15
 

Sample Output
 
   
120 78 1765 7CA ERROR 11001 12D687 D071
代码1:调用上述函数
package acm;

import java.util.Scanner;

public class P1335_2 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			String s=sc.next();
			int num1=sc.nextInt();
			int num2=sc.nextInt();
			int num=Integer.parseInt(s, num1);
			//返回参数num1为基数的将字符串s解析为有符号整数
			String str=Integer.toString(num, num2).toUpperCase();
			//返回第二个参数指定基数表示的第一个参数的字符串表示形式
			if(str.length()>7){
				System.out.println("  ERROR");
			}else{
				System.out.println(String.format("%7s", str));
			}
		}
	}
}
代码2:
自己转换:
package acm;

import java.util.Scanner;

public class P1335 {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			String str1=sc.next();
			String str2=sc.next();
			String str3=sc.next();
			char a[]=str1.toCharArray();
			int x=Integer.parseInt(str2);
			int y=Integer.parseInt(str3);
			String str="";
			if(x==y){
				str=str1;	//如果两个数相等,则不用转换
				if(str1.length()>7){
					System.out.println("  ERROR");
					continue;
				}
			}else{
				str=toExchange(a,x,y);
			}
			if(str.length()>7){
				System.out.println("  ERROR");
			}else{
				System.out.println(String.format("%7s", str));
			}
		}
		
	}

	private static String toExchange(char[] a, int x, int y) {
		String str="";
		char b[]={'A','B','C','D','E','F'};
		String c[]={"A","B","C","D","E","F"};
		int sum=0;	//转换成十进制的值
		for(int i=0;i='0'&&a[i]<='9'){
				sum+=(a[i]-'0')*Math.pow(x, a.length-i-1);
			}else{
				for(int j=0;j<6;j++){
					if(a[i]==b[j]){
						sum+=(10+j)*Math.pow(x, a.length-i-1);
					}
				}
			}
		}
		if(y==10){
			return sum+"";
		}else{//在把十进制转换成相应进制
			String str1[]=new String[30];
			int i=0;
			while(sum!=0){
				if(sum%y<10){
					str1[i]=sum%y+"";
				}else{
					str1[i]=c[sum%y-10];
				}
				sum=sum/y;
				i++;
			}
			for(int j=i-1;j>=0;j--){
				str+=str1[j];
			}
			return str;//返回转换成功后的字符串
		}
	}
}


你可能感兴趣的:(HDOJ水过的那些题)