Exam questions basic practice decimal to hexadecimal

Exam questions basic practice decimal to hexadecimal

Problem Description

Hexadecimal number is a representation of integer that is often used in program design. It has 16 symbols of 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F , representing the decimal numbers 0 to 15 . The hexadecimal counting method is full hexadecimal 1 , so the decimal number 16 is 10 in hexadecimal, and decimal 17 is 11 in hexadecimal, and so on, decimal 30 is in hexadecimal The system is 1E.

Given a non-negative integer, express it in hexadecimal form.

Format description

Input format:

The input contains a non-negative integer a, which represents the number to be converted. 0<=a<=2147483647

Output format:

Output the hexadecimal representation of this integer

Test case

Sample input:

30

Sample output:

1E

Test case analysis

Enter the decimal number represented by the string Binary Hexadecimal
Test case 1 0 0000 0
Test case 2 30 00011110 1E
Test case 3 456 1 1100 1000 1C8
Test case 4 78945 1 0011 0100 0110 0001 1 3461
...... ...... ...... ......

Question thinking

Here I mainly used the following methods:

First: The Long class is used to prevent overflow.

Second: Use String toUpperCase(Locale locale) to capitalize all output characters in this character String using the given rules.

Third: static long parseLong(String s, int radix)
parses the string parameter into a signed long in the base specified by the second parameter.

Fourth: static String toBinaryString(long i) returns the string representation of the long parameter as an unsigned integer in base 2.

Fifth: static Long valueOf(String s, int radix) returns a
Long object, which saves the value from the specified String String and parses it with the radix given by the second parameter.

The above knowledge is done with the help of Java API. I suggest you to check the Java API when you study. Below is the API I use, I hope it will be helpful to you.

API download

Baidu network disk link: https://pan.baidu.com/s/1fuzj93-nleSeH6dViQJszw
Extraction code: rr7r

The procedure is as follows

The first program, on the Blue Bridge Cup system, only gave 45 points. Gu is not perfect yet and needs to continue thinking.

code show as below:

import java.util.Scanner;

public class Main{
    
    
	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		String line = input.next();
		//System.out.println(tenToBinary(binToHex(line)));
		System.out.println(tenToBinary(line));
		System.out.println(binToHex(tenToBinary(line)));
	}
	
	//将十进制转换为二进制
	public static String tenToBinary(String string){
    
    
		return formatBinAddZero(Long.toBinaryString(Long.parseLong(string, 10)));
	}
	
	//将二进制转换为十六进制
	public static String binToHex(String bin){
    
    
		return Long.toHexString(Long.valueOf(bin, 2));
	}
	
	//将二进制的添加零情况
	public static String formatBinAddZero(String string){
    
    
		if(string.length() % 4 == 1){
    
    
			string = "000" + string;
		}
		if(string.length() % 4 == 2){
    
    
			string = "00" + string;
		}
		if(string.length() % 4 == 3){
    
    
			string = "0" + string;
		}
		return string;
	}
}

After my analysis, the reason is that the default output of the console is lowercase letters, so an error will be reported, and Gu can only get 45 points.

Wrong program analysis

In the above code, I rarely added a method to convert the lowercase letters output by the console to uppercase, so I made an error.

The more important point is: the code is not concise enough, the robustness is poor, the code is relatively redundant, it is not recommended to use, you can refer to it.

Source of inspiration for modifying the code

The modified (correct) code is as follows:

import java.util.Scanner;

public class Main {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		String line = input.next();
		//观察,输入的十进制字符串能否正确的转换为二进制
		//System.out.println(tenToBinary(line));
		System.out.println(binToHex(tenToBinary(line)).toUpperCase());
	}
	
	//将十进制转换为二进制
	public static String tenToBinary(String string){
    
    
		return formatBinAddZero(Long.toBinaryString(Long.parseLong(string, 10)));
	}
	
	//将二进制转换为十六进制
	public static String binToHex(String bin){
    
    
		return Long.toHexString(Long.valueOf(bin, 2));
	}
}

In this code, compared to the above code, I only added a method **toUpperCase()** that converts lowercase letters to uppercase letters.

Can learn from other blogs, very simple

Simple writing

Enlightenment

Do more practice, read more, learn data structures and algorithms, and be able to write very well and perfect code.

You also need a basic understanding of the usage of some basic classes. For example , the difference between Integer and Long

Must learn to convert between the bases

Basic base conversion method

Must be careful

Guess you like

Origin blog.csdn.net/weixin_45619069/article/details/113086484