Blue Bridge Cup basic exercise--16 binary to octal

Source: Internet
Author: User
The problem describes the given n hexadecimal positive integers, outputting their corresponding octal numbers. Enter the first behavior of the input format as 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. The output format outputs n rows, and each behavior enters a corresponding octal positive integer. Note that 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 indicates that the hexadecimal number is converted to a binary number, and then converted to octal by a number of digits.
Code: Paste the C + + code First:
#include <iostream> #include <string> using namespace std;
	void Toox (string s) {int t = s.length ()% 3;
		if (t! = 0) {for (int i = 0; i < 3-t; ++i) {s = ' 0 ' + s;
	}} int sum = 0;
	BOOL Notz = false;
		for (int i = 0; i < s.length (); i + = 3) {sum = 0;
		for (int j = 0; j < 2; ++j) {sum + = (s[i + j] = = ' 0 ')? 0:2 * (2-J); } sum + = (s[i + 2] = = ' 0 ')?
		0:1;
		if (sum = = 0 &&!notz) {Notz = true;
			} else {Notz = true;
		cout << sum;
}} cout << Endl;
	} string ToBin (String s) {String st = "";
			for (int i = 0; i < s.length (), ++i) {switch (S[i]) {case ' 0 ': st + = "0000";
		Break
			Case ' 1 ': St + + "0001";
		Break
			Case ' 2 ': St + + "0010";
		Break
			Case ' 3 ': St + + "0011";
		Break
			Case ' 4 ': St + + "0100";
		Break
			Case ' 5 ': St + + "0101";
		Break
			Case ' 6 ': St + + "0110";
		Break
			Case ' 7 ': St + + "0111";
		Break
			Case ' 8 ': St + + "1000"; BreAk
			Case ' 9 ': St + + "1001";
		Break
			Case ' A ': St + + "1010";
		Break
			Case ' B ': St + + "1011";
		Break
			Case ' C ': St + + "1100";
		Break
			Case ' D ': st + = "1101";
		Break
			Case ' E ': st + = "1110";
		Break
			Case ' F ': St + + "1111";
		Break
}} return St;
	} int main () {int n;
	CIN >> N;
	String *s = new String[n];
	for (int i = 0; i < n; ++i) {cin >> s[i];
	} for (int j = 0; J < N; ++j) {Toox (ToBin (S[j]));
	} delete[]s;
	String St;
return 0; }

The code behind Java (and the C + + logic is the same):
Import Java.util.Scanner;
		public class Main {public static void main (string[] args) {Scanner in = new Scanner (system.in);
		int n = in.nextint ();
		String s[] = new String[n];
		for (int i = 0; i < n; ++i) {S[i] = In.next ();
		} in.close ();
		for (int i = 0; i < s.length; ++i) {Toox (ToBin (s[i]));
		}} public static String ToBin (string s) {char[] a = S.tochararray ();
		String st = "";
				for (int i = 0; i < s.length (), ++i) {switch (A[i]) {case ' 0 ': st + = "0000";
			Break
				Case ' 1 ': St + + "0001";
			Break
				Case ' 2 ': St + + "0010";
			Break
				Case ' 3 ': St + + "0011";
			Break
				Case ' 4 ': St + + "0100";
			Break
				Case ' 5 ': St + + "0101";
			Break
				Case ' 6 ': St + + "0110";
			Break
				Case ' 7 ': St + + "0111";
			Break
				Case ' 8 ': St + + "1000";
			Break
				Case ' 9 ': St + + "1001";
			Break
				Case ' A ': St + + "1010";
			Break
				Case ' B ': St + + "1011";
			Break
				Case ' C ':St + = "1100";
			Break
				Case ' D ': st + = "1101";
			Break
				Case ' E ': st + = "1110";
			Break
				Case ' F ': St + + "1111";
			Break
	}} return St;
		} public static void Toox (String s) {int t = s.length ()% 3;
			if (t! = 0) {for (int i = 0; i < 3-t; ++i) {s = 0 + s;
		}} int sum = 0;
		Boolean Notz = false;
		Char[] A = S.tochararray ();
			for (int i = 0; i < s.length (); i + = 3) {sum = 0;
			for (int j = 0; j < 2; ++j) {sum + = (a[i + j] = = ' 0 ')? 0:2 * (2-J); } sum + = (a[i + 2] = = ' 0 ')?
			0:1;
			if (sum = = 0 &&!notz) {Notz = true;
				} else {Notz = true;
			System.out.print (sum);
	}} System.out.println ();
 }
}

Here is the question, I submitted a total of 16 times, I started to submit only Java version, but has been running timeout, and then I started to submit C + + version, after several attempts, the final commit success, 100 points, 109ms, is the above code, and then I rewrite into Java code, and then submit , but it runs out of time and finally has to abandon the Java version.
Because I submitted 16 times, so here more to say, about this algorithm, initially I think is hexadecimal first converted to decimal, in the conversion into octal, because there is a question is hexadecimal to decimal, so that can steal lazy, but submitted a few times always error, and then carefully read the problem found " Each hex number length does not exceed 100000 ", that means that the test data will have at least one length is 100000 hexadecimal number, so the idea of converting to decimal is wrong, should be converted to binary, a hexadecimal number is four-bit binary, three-bit binary is an octal, Should follow this idea.
The simplest way to do this is to convert the 16 binary into a binary string, and then use switch to turn the three-bit binary into an octal, which I tried several times with Java, but still timed out, eventually giving up, and then thinking that I could calculate the octal by arithmetic method (I don't know which method is faster, I didn't use the switch method mentioned earlier in C + +, just like the code says. Finally several attempts, finally succeeded. Also because the speed requirements are relatively high, so try to operate on the original string, can not extract the string is not extracted, I have tried to use similar substring method in the long string to extract three characters, but timed out. There are also a few times because of their negligence and led to the operation of the error, the next time more careful, warning good.
(last update, Java code, in the Tobin method, the string object St is changed to StringBuffer, the + = is changed to append (). Timeout problem solved)

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.