Blue Bridge Cup 16 binary conversion 8 binary
I said I was too slag, always timed out, and couldn't pass the test.
Topic
Problem description
Given n hexadecimal positive integers, output their corresponding octal numbers.
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, each hexadecimal number is not more than 100000.
Output format
Outputs n rows, each of which enters a corresponding octal positive integer.
Attention
The hexadecimal number entered does not have a leading 0, such as 012A.
The octal number of the output cannot have a leading 0.
Sample input
2
39
123ABC
Sample output
71
4435274
Tips
The hexadecimal number is converted to a number of decimal digits, and then a binary number is converted into octal.
Ideas
At first, there is no idea, using the most primitive approach, the first conversion of 16 into 10, and then from 10 to 8 into the system, the result is wrong. Download test Data , only to find the data so perverted, change BigInteger
to store data, the results run out of time, after thinking, found in accordance with the traditional way, is unable to pass the Internet to find the answer, to find the skills of conversion.
From the base of the binary conversion, the binary conversion shows that you can convert 16 binary to 2, and then the conversion from 2 to 8, as to why this is done, because the numbers on each of the 16 decimal places can become 4 bits of 2 binary, and every 3 binary number can be composed of 8 binary corresponding to the number of seats .
And then by the number of logic to calculate the 2 binary method:8421 method , corresponding to 1
the position of the corresponding number, the results should be able to come out. And I followed the idea of writing, or run timeout . In the end, I found out that my algorithm was not optimized enough to see the example of the reference .
Test data
Perverted test Data 16 ext. 10, 10 ext. 8
/* * This is 16 binary converted to 10 and then converted to a 8 binary timeout, */public void Convert16from10to8 (String num16) {int flag = 0; BigInteger sum = new BigInteger ("0"); BigInteger tmp = new BigInteger ("16"); 16 first converted to 10 binary char[] Charr = Num16.tochararray (); for (int x = charr.length-1; x >= 0, x--) {switch (charr[x]) {case ' A ': Case ' B ': Case ' C ': Case ' D ': Case ' E ': Case ' F '://Sum + = (charr[x]-' 0 ' -7) * MATH.POW (flag++); sum = Sum.add (Tmp.pow (flag++) Multiply (new BigInteger ((charr[x]-' 0 '-7) + "")); Break Default://Sum + = (charr[x]-' 0 ') * MATH.POW (flag++); sum = Sum.add (Tmp.pow (flag++) Multiply (new BigInteger ((charr[x]-' 0 ') + "")); Break }} System.out.println (sum); StringBuffer SB = new StringBuffer (); while (Sum.intvalue () > 8) {//Sb.append (SUM%8); Sb.insert (0, Sum.remainder (New BigInteger ("8")); sum = sum.divide (New BigInteger ("8")); } sb.insert (0, sum); Sb.append (sum); System.out.println (Sb.tostring ()); }
16 ext. 2, 2 ext. 8
/* * 16 into 2 binary, then 8 16--"2 The number on each bit can be converted to 4 bits of 2 binary * Every three of the 2 binary combinations are converted to 8 binary on each bit on the number, not enough to fill 0 */public void Conv Ert16from2to8 (String num16) {char[] Charr = Num16.tochararray (); int tmp = 0; StringBuffer sbsum = new StringBuffer (); for (int x = 0; x < charr.length; + +) {switch (Charr[x]) {//character corresponding integer case ' A ': case ' B ': Case ' C ': Case ' D ': Case ' E ': Case ' F ': tmp = charr[x]-' 0 '-7; Break Default:tmp = charr[x]-' 0 '; Break } stringbuffer sb = new StringBuffer (); Convert to Binary while (TMP >= 2) {sb.insert (0, tmp% 2); TMP/= 2; } sb.insert (0, TMP); System.out.println (Sb.length ()); int len = 4-sb.length ();//If you write directly inside the for loop, the SB changes, causing Len to change for (int y = 0; y < len; y+ +) Sb.insert (0, 0); System.out.println (Sb.tostring ()); Sbsum.append (SB); }//System.out.println (sbsum.tostring ()); StringBuffer sbsum8=new StringBuffer ();//Record the final result int tmp8item=0; Every 3 groups, not enough to enjoy high 0, that is, the leftmost 0, using 421,//or a 3 to do the cycle, divided into areas, 1, according to the method of 421 add char[] chArr2 = sbsum.tostring (). ToChar Array (); 1001 for (int z = charr2.length-1, num3 = 0; z >= 0; z--) {if (Charr2[z]-' 0 ' = = 1) { Switch (num3) {case 0:tmp8item+=1; Break Case 1:tmp8item+=2; Break Case 2:tmp8item+=4; Break }} if ((num3+1)%3==0) {sbsum8.insert (0, Tmp8item); tmp8item=0; } num3= (num3+1)%3; } if (sbsum8.substring (0, 1). equals("0")) The octal number of the output cannot have a leading 0 judgment System.out.println (sbsum8.substring (1,sbsum8.length ())); Else System.out.println (sbsum8.tostring ()); }
Other people's ideas
The 1-bit 16 binary can represent 4-bit 2-binary, 1-bit 8-binary can represent a 3-bit binary, which results in a 3-bit 16-sum-in- stack output representing 4-bit 8- binary, and then out-of-stack output.
My understanding is:
3-bit 16 binary, one 16 binary available in 4-bit 2 notation, i.e.: 34=12.
4-bit 8 binary, one 8 binary available in 3-bit 2 notation, i.e.: 43-12
So the two of them are equivalent .
Complete code (can be tested):
Import Java.util.scanner;public class Main {public static void Main (string[] args) {new Main (). Systemscanner ();} PU Blic void Systemscanner () {Scanner Jin = new Scanner (system.in); while (Jin.hasnext ()) {int length = Jin.nextint (); for (int i = 0; i < length; i++) {String strtmp=jin.next (); Tranform (Strtmp.tochararray (), strtmp.length ()); }}}//* 3-bit 16 binary equivalent to 4-bit 8-binary */int[] stack=new int[40000]; public void Tranform (char[] str, int length) {char[] buff = new CHAR[4]; int top =-1; for (int i = length-1; I >= 0; I-= 3) {int sum = 0; for (int j = 0; J < 3 && i-j >= 0; j + +) {//i-j>=0 prevent insufficient three cases int tmp = str[i-j] >= ' 0 ' && Str[i-j] <= ' 9 '? STR[I-J]-' 0 ': str[i-j]-' A ' + 10;//is the number, or character, that corresponds to the conversion sum+= (tmp<< (4*J));//This sentence is very Important, through this sentence can be changed from 16 into 10, but, do not know why? How did that come about? } stack[++top]=sum;/The result of the/sum is 16 binary conversion 10 binary results, each 3 16 binary into 10, and then 8 binary} while (stack[top]==0) {//Exclude preamble to 0 of the judgment top--; }//for (int i=top;i>=0;i--) {///Direct output will lose leading 0 because this conversion to 8 binary is not the leftmost case and should be preserved 0//System.out.print (integer.tooctals Tring (Stack[i]));//convert from 10 to 8//} for (int i=top;i>=0;i--) {String str1=integer.tooctalstring (stack[ I]);//converted from 10 to 8 if (I!=top&&str1.length () <4) {///not the leftmost one, without removing the leading 0, and the default is to remove 0, so to make a fill for (int y=0;y<4-str1.length (); y++) System.out.print ("0"); } System.out.print (STR1); } System.out.println (); }}
Examples of references adopted"Blue Bridge Cup" 16 conversion 8 Binary
Expand your knowledge
- [JAVA] binary, octal, hexadecimal, decimal between conversions, using classes
Integer
to convert between the binary.
Sprintf equivalent in Java
Requirements for, C code forchar buff[4];sprintf(buff ,"%o", stack[i]);
The corresponding java
code is// Store the formatted string in ‘result‘String result = String.format("%4d", i * j);// Write the result to standard outputSystem.out.println( result );
Blue Bridge Cup 16 binary conversion 8 binary