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
" hint "
Converts the hexadecimal number to a certain number, and then converts it to an octal number.
This problem blogger I did for a long time, after a few changes have finally passed the test.
The first time I submitted it, I tested it with Eclipse, and the goose ...
Run timed out ...!!
Let's start with my previous code.
Package hexadecimaltooctal;
Import Java.util.Scanner;
The public class Main {/** * problem describes the given n hexadecimal positive integers, outputting their corresponding number of octal.
The first behavior of input format 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.
The output format outputs n rows, and each behavior enters the corresponding octal positive integer.
* @param args */public static void main (string[] args) {Scanner input = new Scanner (system.in);
int n = input.nextint ();
String[] in = new String[n];
String[] out = new String[n];
for (int i = 0; i < n; i++) {In[i] = Input.next ();
int len = In[i].length ();
int j = len%3;
String Strin = "";
String strout = "";
if (j = = 1) {Strin = In[i].charat (0) + "";
Strout = trans (Strin);
else if (j = = 2) {Strin = In[i].charat (0) + "" + in[i].charat (1) + "";
Strout = trans (Strin);
else {strout = ""; while (J < In[i].length ()) {Strin = In[i].charat (j) + "" + In[i].charat (j + 1) + "" + In[i].charat (j + 2)
+ "";
Strout + + trans (strin);
J + + 3;
}
Out[i] = Strout;
for (string string:out) {System.out.println (string);
} input.close (); private static string trans (string strin) {//return integer.tostring (Integer.parseint (strin,16), 8); &NBSP;&N Bsp //hexadecimal binary string bs = integer.tobinarystring ( Integer.valueof (Strin, 16)); //binary turn octal String os = Integer.tooctalstring ((Integer.valueof (BS, 2));
return os; }
}
And here's the test results on my eclipse:
The first time I submitted the code of thought is correct, wrong is wrong in my main function in the use of people with their own conversion function!!! This is the key point of the timeout.
Emmm, helpless system has running time and memory limitations, I can only implement the method of conversion.
The code has been shelved for two days I spent hours changing the code from the beginning to the end (don't laugh at me).
After a few twists and ends finally passed the evaluation.
Cough, not mealy, directly on the code ~
Import Java.util.Scanner;
public class Main {public static void main (string[] args) {/* input */Scanner input = new Scanner (system.in);
int n = input.nextint ();
string[] str = new String[n];
for (int i = 0; i < n; i++) {Str[i] = Input.next ();
} input.close ();
StringBuffer sb = new StringBuffer (); for (int i = 0; i < n; i++) {/* hexadecimal binary */for (int j = 0; J < Str[i].length (); j +) {Char ch = str
[I].charat (j);
Switch (CH) {case ' 0 ': sb.append ("0000");
Break
Case ' 1 ': Sb.append ("0001");
Break
Case ' 2 ': Sb.append ("0010");
Break
Case ' 3 ': Sb.append ("0011");
Break
Case ' 4 ': Sb.append ("0100");
Break
Case ' 5 ': Sb.append ("0101");
Break
Case ' 6 ': Sb.append ("0110");
Break
Case ' 7 ': Sb.append ("0111");
Break
Case ' 8 ': Sb.append ("1000");
Break
Case ' 9 ': Sb.append ("1001");
Break Case 'A ': Sb.append ("1010");
Break
Case ' B ': sb.append ("1011");
Break
Case ' C ': Sb.append ("1100");
Break
Case ' D ': Sb.append ("1101");
Break
Case ' E ': Sb.append ("1110");
Break
Case ' F ': sb.append ("1111");
Break }/* Binary hex/if (sb.length ()% 3 = 0) {if (sb.substring (0, 3). Equals (")" {sb.de
Lete (0, 3);
} else if (Sb.length ()% 3 = 1) {if (sb.substring (0, 1). Equals ("0")) {sb.delete (0, 1);
else {sb.insert (0, "00");
} else if (Sb.length ()% 3 = 2) {if (sb.substring (0, 2). Equals (")" {sb.delete (0, 2);
else {sb.insert (0, "0");
} int[] C = new Int[sb.length ()/3];
int m = 0;
for (int k = 0; k < sb.length (); k+=3) {String string = Sb.substring (k, k + 3);
Switch (string) {case "n": c[m++] = 0;
Break
Case "001": c[m++] = 1;
Break CASE "010": c[m++] = 2;
Break
Case "011": c[m++] = 3;
Break
Case "the": c[m++] = 4;
Break
Case "a": c[m++] = 5;
Break
Case "a": c[m++] = 6;
Break
Case "a": c[m++] = 7;
Break
}/* Print output/for (int j = 0; J < m; J +) {System.out.print (c[j]);
} System.out.println ();
Sb.delete (0, Sb.length ()); }
}
}
The above code has a point to note.
After the first number completes the hexadecimal to octal conversion, don't forget to empty the buffer, otherwise stringbuffer as a transit point. The number of the front numbers in the conversion to the second number is still present in the StringBuffer as part of the following number, resulting in ... Everyone knows ~
Then directly on the result map, there is no understanding of the welcome message or DMS Oh ~
Like just please pay attention to me, your attention is my greatest motivation ~
Related Code Links: https://github.com/striner/javaCode/blob/master/hexadecimalToOctal