Blue Bridge Cup Title 2 - Hexadecimal to Octal

Posted by djdog.us on Sun, 26 Jan 2020 06:28:08 +0100

Hexadecimal to Octal

1 Topic

Title: Hexadecimal to Octal
Problem Description
Given n n n positive hexadecimal integers, output their corresponding octal numbers.
  
Input Format
The first line of input is a positive integer n (1<=n<=10).
The next n lines, each with a string of 09 uppercase letters AF, represent positive hexadecimal integers to be converted, each hexadecimal number no longer than 100000 in length.

Output Format
Output n rows, each input corresponding to a positive octal integer.

[Attention]
The hexadecimal number entered will not have a leading 0, such as 012A.
The octal number of the output cannot have a leading 0.

2 Solving process

2.1 First Edition Code

Lazy to write the first version directly using java's transformation function and try to commit.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) { 
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		scanner.nextLine(); // Get a line break after nextInt
		for (int i = 0; i < n; i++) { 
			// Use Integer encapsulated conversion functions directly
			// Convert hexadecimal to 10, then 10 to 8
			System.out.println(Integer.toOctalString(Integer.parseInt(scanner.nextLine(), 16)));
		}
		scanner.close();
	}
}

It's OK to test it yourself.But if you submit it it, it will show you the error directly. Download the input data and look at it.

The data is really large, after all, the title says that hexadecimal numbers are within 100,000 digits.(But what's the first action? No line breaks?Is it a problem with txt documentation?It is estimated that this is the reason for the data being too long, because the 16-bit conversion to the 10-bit int type during java's functions is absolutely not what the 100,000-bit 16-bit conversion to the 10-bit int type can hold.It seems that 16 to 8 is more troublesome than the other two 16 to 10 and 10 to 16.

2.2 StringBuffer,StringBuilder,String

Reference again Other people's code Or you have to use string storage.Then I wrote a note with String that I found it was not possible to understand the differences between StringBuffer, StringBuilder and String.Previously, although you knew that Strings in C# were also immutable, each modification created a new String.String in Java, however, can't be modified at all after it's created.This leads to StringBuffer and StringBuilder that can be modified.The three of them Difference Probably as follows.

  • String, cannot be modified after creation.
  • StringBuffer, modifiable, thread safe, slower than StringBuilder.
  • StringBuilder, modifiable, thread insecure, faster than StringBuffer.

Now you need to create a string that is constantly being modified and use StringBuilder.

2.3 String converted from hexadecimal to binary

In fact, directly judge the characters read in, write the conversion relationship between hexadecimal and binary, and connect the binary strings.

String hexNum = scanner.nextLine();
StringBuilder binaryNum = new StringBuilder();
for (int j = 0;j < hexNum.length(); j++) {
	switch (hexNum.charAt(j)) {
	case '0':binaryNum.append("0000");break;
	case '1':binaryNum.append("0001");break;
	case '2':binaryNum.append("0010");break;
	case '3':binaryNum.append("0011");break;
	case '4':binaryNum.append("0100");break;
	case '5':binaryNum.append("0101");break;
	case '6':binaryNum.append("0110");break;
	case '7':binaryNum.append("0111");break;
	case '8':binaryNum.append("1000");break;
	case '9':binaryNum.append("1001");break;
	case 'A':binaryNum.append("1010");break;
	case 'B':binaryNum.append("1011");break;
	case 'C':binaryNum.append("1100");break;
	case 'D':binaryNum.append("1101");break;
	case 'E':binaryNum.append("1110");break;
	case 'F':binaryNum.append("1111");break;
	default:break;
	}
}

2.4 Converts a binary string to an octal

// Ensure that the binary string is a multiple of 3
while(binaryNum.length() % 3 != 0) {
	binaryNum.insert(0, '0'); // Add a 0 before the length when the remainder of 3 is not equal to 0
}

// Convert to 8ary
StringBuilder octNum = new StringBuilder();
for (int j = 0; j < binaryNum.length(); j += 3) {
	String temp = binaryNum.substring(j, j+3);
	switch (temp) { 
		// Judge and add every three characters as a substring
		case "000": octNum.append("0"); break;
		case "001": octNum.append("1"); break;
		case "010": octNum.append("2"); break;
		case "011": octNum.append("3"); break;
		case "100": octNum.append("4"); break;
		case "101": octNum.append("5"); break;
		case "110": octNum.append("6"); break;
		case "111": octNum.append("7"); break;
		default: break;
	}
}

// Eliminate Leading 0
while(octNum.charAt(0) == '0') {
	octNum.deleteCharAt(0);	// Delete the first number when the first number is 0
}

2.5 Complete Code

At last it ran successfully.531ms, 42.92MB.Feel so high. Feel like it's going to time out anytime.

import java.util.Scanner;

public class Main {
	public static void main(String[] args) { 
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		scanner.nextLine();
		
		for (int i = 0; i < n; i++) { 
			String hexNum = scanner.nextLine();
			StringBuilder binaryNum = new StringBuilder();
			for (int j = 0;j < hexNum.length(); j++) {
				switch (hexNum.charAt(j)) {
				case '0':binaryNum.append("0000");break;
				case '1':binaryNum.append("0001");break;
				case '2':binaryNum.append("0010");break;
				case '3':binaryNum.append("0011");break;
				case '4':binaryNum.append("0100");break;
				case '5':binaryNum.append("0101");break;
				case '6':binaryNum.append("0110");break;
				case '7':binaryNum.append("0111");break;
				case '8':binaryNum.append("1000");break;
				case '9':binaryNum.append("1001");break;
				case 'A':binaryNum.append("1010");break;
				case 'B':binaryNum.append("1011");break;
				case 'C':binaryNum.append("1100");break;
				case 'D':binaryNum.append("1101");break;
				case 'E':binaryNum.append("1110");break;
				case 'F':binaryNum.append("1111");break;
				default:break;
				}
			}
			
			while(binaryNum.length() % 3 != 0) {
				binaryNum.insert(0, '0');
			}
			
			StringBuilder octNum = new StringBuilder();
			for (int j = 0;j < binaryNum.length();j+=3) {
				String temp = binaryNum.substring(j, j+3);
				switch (temp) {
				case "000": octNum.append("0"); break;
				case "001": octNum.append("1"); break;
				case "010": octNum.append("2"); break;
				case "011": octNum.append("3"); break;
				case "100": octNum.append("4"); break;
				case "101": octNum.append("5"); break;
				case "110": octNum.append("6"); break;
				case "111": octNum.append("7"); break;
				default: break;
				}
			}
			
			while(octNum.charAt(0) == '0') {
				octNum.deleteCharAt(0);				
			}
			System.out.println(octNum);
		}
		scanner.close();
	}
}
Twelve original articles have been published. Praise 5. Visits 925
Private letter follow

Topics: Java