Waterloo Cup: Basic practice hex Turn octal

Source: Internet
Author: User

Basic practice hexadecimal octal time limit: 1.0s memory Limit: 512.0MB problem description
Given n hexadecimal positive integers, output their corresponding number of octal.

Input format
The first behavior of the input is a positive integer n (1<=n<=10).
Next n rows, each line a string of 0~9, uppercase letters A~F, representing the hexadecimal positive integer to be converted, with a length of no more than 100000 per hexadecimal.

Output format
Output n rows, and each behavior enters the corresponding octal positive integer.

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

Sample input
2
39
123ABC

Sample output
71
4435274

Prompted
Converts the hexadecimal number to a certain number, and then converts it to an octal number.

First, use the encapsulated method in Java to convert the 16 to decimal, and then convert the decimal system to octal.

Import Java.util.Scanner;

public class Basictranformsixteentoeight {public

	static void transform () {
		Scanner sc = new Scanner (system.in); C3/>int n = sc.nextint ();
		string[] Arry = new STRING[10];
		if (n >= 1 && n <=) {for
			(int i = 0; i < n; i++) {
				String number = Sc.next ();
				if (Number.length () < 100000) {
					Arry[i] = Integer
							. tooctalstring (integer.valueof (number));
				}
			for
			(int i = 0; i < n; i++) {
				System.out.println (arry[i]);
			}
	} public static void Main (string[] args) {
          transform ();
}
}
can be implemented, but is used to test data but output errors, mainly because the length of the integer is limited. Does not satisfy the condition of the hexadecimal string length <100000.

After that, write your own algorithm implementation: the implementation of the algorithm: the first 16 into the binary, each 16 into 4-bit binary, the converted binary string to fill 0 correction. If its length mod 3 = 2, then fill in a "0" correction in front of it, if its length mod 3 = 1, then fill in the "00" correction, so that it can be successfully converted to octal string, each three-bit binary conversion to a binary. Successful transformation of the octal string does not meet the requirements of the title, the problem requires no leading 0 output, so you need to go to the leading 0 operations. Use substring () to get the leading of the octal string, and to determine if it is 0, if 0, go to the leading 0 output, otherwise direct output.

Import Java.util.Scanner; public class Basictranformsixteentoeight {public static void Binarytransform (int n, char[][] Arry, string[] Arrybinaryst
		R) {string[] arryoctalstr = new STRING[10];
		String str = "";
			for (int i = 0; i < n; i++) {Arrybinarystr[i] = ""; for (int j = 0; J < Arry[i].length; j +) {switch (Arry[i][j]) {case ' 0 ': arrybinarystr[i] = Arrybinaryst
					R[i] + "0000";
				Break
					Case ' 1 ': arrybinarystr[i] = Arrybinarystr[i] + "0001";
				Break
					Case ' 2 ': arrybinarystr[i] = Arrybinarystr[i] + "0010";
				Break
					Case ' 3 ': arrybinarystr[i] = Arrybinarystr[i] + "0011";
				Break
					Case ' 4 ': arrybinarystr[i] = Arrybinarystr[i] + "0100";
				Break
					Case ' 5 ': arrybinarystr[i] = Arrybinarystr[i] + "0101";
				Break
					Case ' 6 ': arrybinarystr[i] = Arrybinarystr[i] + "0110";
				Break
					Case ' 7 ': arrybinarystr[i] = Arrybinarystr[i] + "0111";
				Break Case ' 8 ': arrybinarystr[I] = Arrybinarystr[i] + "1000";
				Break
					Case ' 9 ': arrybinarystr[i] = arrybinarystr[i] + "1001";
				Break
					Case ' A ': arrybinarystr[i] = Arrybinarystr[i] + "1010";
				Break
					Case ' B ': arrybinarystr[i] = Arrybinarystr[i] + "1011";
				Break
					Case ' C ': arrybinarystr[i] = arrybinarystr[i] + "1100";
				Break
					Case ' D ': arrybinarystr[i] = Arrybinarystr[i] + "1101";
				Break
					Case ' E ': arrybinarystr[i] = Arrybinarystr[i] + "1110";
				Break
					Case ' F ': arrybinarystr[i] = Arrybinarystr[i] + "1111";
				Break for (int i = 0; i < n; i++) {if (Arrybinarystr[i].length ()% 3 = 1) {Arrybinarystr[i] = "+" + A
			Rrybinarystr[i];
			else if (arrybinarystr[i].length ()% 3 = 2) {Arrybinarystr[i] = "0" + arrybinarystr[i];
			} Arryoctalstr[i] = "";
				for (int j = 0; J < Arrybinarystr[i].length ()/3; j +) {str = arrybinarystr[i].substring (J * 3, J * 3 + 3); Switch (str) {CASe "": arryoctalstr[i] + = "0";
				Break
					Case "001": arryoctalstr[i] + = "1";
				Break
					Case "010": arryoctalstr[i] + = "2";
				Break
					Case "011": arryoctalstr[i] + = "3";
				Break
					Case "the": arryoctalstr[i] + + + 4;
				Break
					Case "": arryoctalstr[i] + = "5";
				Break
					Case "the": arryoctalstr[i] + + + 6;
				Break
					Case "": arryoctalstr[i] + = "7";
				Break
			for (int i = 0; i < n; i++) {str = arryoctalstr[i].substring (0, 1);
			if (Str.equals ("0")) {System.out.println (arryoctalstr[i].substring (1, Arryoctalstr[i].length ()));
			else {System.out.println (arryoctalstr[i]);
		}} public static void Main (string[] args) {Scanner sc = new Scanner (system.in);
		int n = sc.nextint ();
		char[][] Arry = new char[10][100000];
		string[] Arrybinarystr = new STRING[10];
		String number; if (n >= 1 && n <=) {for (int i = 0; i < n; i++){number = Sc.next ();
			Arry[i] = Number.tochararray ();
		} binarytransform (n, Arry, ARRYBINARYSTR);
 }
	}
}

After the above algorithm is implemented, it is true to successfully implement the function of converting 16 to octal, and also satisfies the condition of hexadecimal string length <100000, but it runs timeout. Finally find data found that the main reason is that the algorithm needs to implement string concatenation, and this algorithm directly using the connector "+" implementation, each operation will produce a new string object, after the improvement of the use of StringBuilder to create objects, the use of append () method to implement string concatenation.

Import Java.util.Scanner; public class Basictranformsixteentoeight {public static void Tobinarytransform (int n, char[][] Arry, string[] Arrybi
		NARYSTR) {string[] arryoctalstr = new STRING[10];
		String str = "";
		StringBuilder octalstb = new StringBuilder ();
		StringBuilder binarystb = new StringBuilder ();
		/*string[] Arryoctalbinarystr = {"," 001 "," 010 "," 011 "," M "," a "," a "," ""};*/int i, J, K, Len;
			for (i = 0; i < n; i++) {Arrybinarystr[i] = "";
			Arryoctalstr[i] = "";
			len = arry[i].length; for (j = 0; J < Len; J + +) {//convert Hex into Binary switch (arry[i][j)} {case ' 0 ': binarystb.append ("00
					00 ");
				Break
					Case ' 1 ': Binarystb.append ("0001");
				Break
					Case ' 2 ': Binarystb.append ("0010");
				Break
					Case ' 3 ': Binarystb.append ("0011");
				Break
					Case ' 4 ': Binarystb.append ("0100");
				Break
					Case ' 5 ': Binarystb.append ("0101");
				Break Case ' 6 ': BinarystB.append ("0110");
				Break
					Case ' 7 ': Binarystb.append ("0111");
				Break
					Case ' 8 ': Binarystb.append ("1000");
				Break
					Case ' 9 ': Binarystb.append ("1001");
				Break
					Case ' A ': Binarystb.append ("1010");
				Break
					Case ' B ': binarystb.append ("1011");
				Break
					Case ' C ': Binarystb.append ("1100");
				Break
					Case ' D ': Binarystb.append ("1101");
				Break
					Case ' E ': Binarystb.append ("1110");
				Break
					Case ' F ': binarystb.append ("1111");
				Break
				Default:break;
			} Arrybinarystr[i] = Binarystb.tostring ();
			BINARYSTB = new StringBuilder ();
				if (Arrybinarystr[i].length ()% 3 = 1) {//Correct the Binary in order to convert it into octal.
			Arrybinarystr[i] = "more" + arrybinarystr[i];
			else if (arrybinarystr[i].length ()% 3 = 2) {Arrybinarystr[i] = "0" + arrybinarystr[i];
			Len = Arrybinarystr[i].length ()/3; for (j = 0; J < Len; J +) {//convert BiNary into octal str = arrybinarystr[i].substring (J * 3, J * 3 + 3);
					/*for (k = 0; k <= 7; k++) {if (Str.equals (arryoctalbinarystr[k))) {octalstb.append (k);
					}}*/switch (str) {case ": Octalstb.append (" 0 ");
				Break
					Case "001": Octalstb.append ("1");
				Break
					Case "010": Octalstb.append ("2");
				Break
					Case "011": Octalstb.append ("3");
				Break
					Case "the": Octalstb.append ("4");
				Break
					Case "a": Octalstb.append ("5");
				Break
					Case "the": Octalstb.append ("6");
				Break
					Case "a": Octalstb.append ("7");
				Break
			} Arryoctalstr[i] = Octalstb.tostring ();
			OCTALSTB = new StringBuilder ();
			str = arryoctalstr[i].substring (0, 1);
				if (Str.equals ("0")) {//Remove the ' the ' and ' the ' the ' the '
			System.out.println (arryoctalstr[i].substring (1, Arryoctalstr[i].length ()));
			else {System.out.println (arryoctalstr[i]);
		}} public static void Main (string[] args) {Scanner sc = new Scanner (system.in);
		int n = sc.nextint ();
		char[][] Arry = new char[10][100000];
		string[] Arrybinarystr = new STRING[10];
		String number;
				if (n >= 1 && n <=) {for (int i = 0; i < n; i++) {number = Sc.next ();
			Arry[i] = Number.tochararray ();
		} tobinarytransform (n, Arry, ARRYBINARYSTR);
 }
	}
}
The improved algorithm has been tested successfully and the running time is shortened greatly.

When converting to octal, you first direct the string to the switch for comparison and conversion. Check on the internet switch only supports int char byte, and string does not work on the switch statement, and its string is used to switch the real implementation is to convert it to hashcode (return int type), using Equals () method to compare and increase the overhead. So I wrote an array. The binary string is converted to an octal string (the part of the annotation in the above code). The test found that the array increased the overhead, the test results are as follows:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.