Blue Bridge Cup-basic exercise-16 binary to octal (is timed out!!!) )

Source: Internet
Author: User

Basic practice hex to octal time limit: 1.0s memory limit: 512.0MBThe 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.

Brother I tried, or overtime, I did not think! Blue Bridge, the fucking data is so sick! Do you have a download to see his data? My algorithm is the same as the C + + algorithm, but I am timed out! C + + will pass! Java is still not suitable for a problem!
Import Java.io.*;import java.util.*;p ublic class main{public static void Main (string[] args) {//TODO auto-generated Metho D stubscanner input = new Scanner (system.in); int n=input.nextint (); Input.nextline (); for (int i=0;i<n;i++) {String STR1 = Input.nextline ();                              String str2= ""; Used to string the concatenation of char a[] = Str1.tochararray (); Record the string just entered into the character array for (int j=0;j<str1.length (); j + +)//16 binary conversion to binary {switch (A[j]) {case ' 0 ': str2+= "0000"; Case ' 1 ': str2+= "0001"; Break;case ' 2 ': str2+= "0010"; Break;case ' 3 ': str2+= "0011"; Break;case ' 4 ': str2+= "0100"; Case ' 5 ': str2+= "0101"; Break;case ' 6 ': str2+= "0110"; Break;case ' 7 ': str2+= "0111"; Break;case ' 8 ': str2+= "n"; break; Case ' 9 ': str2+= "1001", Break;case ' A ': str2+= "1010"; Break;case ' B ': str2+= "1011"; Break;case ' C ': str2+= "1100"; break; Case ' D ': str2+= "1101"; Break;case ' E ': str2+= "1110"; Break;case ' F ': str2+= "1111"; break;default:break;}}             System.out.println (STR2); Number of digits before the test correction//correction digit int arry[] = new Int[10000001];if (Str2.length ()%3==1) {str2= "xx" +STR2;} if (Str2.length ()%3==2) {str2= "0" +STR2;}              System.out.println (STR2);     Test the corrected number of digits in the case of//system.out.println (Str2.length ());       Test the corrected number of digits!char b[] = Str2.tochararray ();                             After correcting the number of digits, the original A-character array cannot be used!, and replaced by the B-character array int j=0; Record 8 binary digits for (int k=0;k<=str2.length () -3;k+=3) {arry[j]= (b[k]-' 0 ') *4+ (b[k+1]-' 0 ') *2+ (b[k+2]-' 0 '); j + +;} for (int k=0;k<j;++k) {if (k==0&&arry[k]==0) continue; System.out.print (Arry[k]);} System.out.println ();}}}



C++:AC Code!
#include <iostream> #include <string>using namespace Std;int arr[10000001];int main () {int n,len_str,i,j;    String str,str2;    cin>>n;        while (n--) {cin>>str;        Len_str=str.length ();                Str2= ""; 16 binary conversion to binary for (i=0;i<len_str;++i) {switch (Str[i]) {case ' 0 ': str2+= "            0000 "; break;            Case ' 1 ': str2+= "0001";            Case ' 2 ': str2+= "0010";            Case ' 3 ': str2+= "0011";            Case ' 4 ': str2+= "0100";            Case ' 5 ': str2+= "0101";            Case ' 6 ': str2+= "0110";            Case ' 7 ': str2+= "0111";            Case ' 8 ': str2+= "n";            Case ' 9 ': str2+= "1001";            Case ' A ': str2+= "1010";            Case ' B ': str2+= "1011";            Case ' C ': str2+= "1100";            Case ' D ': str2+= "1101";            Case ' E ': str2+= "1110";       Case ' F ': str2+= "1111";     Default:break;                }}//fixed number of digits if (len_str%3==1) str2= "xx" +STR2;                        else if (len_str%3==2) str2= "0" +STR2;        Len_str=str2.length ();        binary conversion octal j=0;            for (i=0;i<=len_str-2;i+=3) {arr[j]= (str2[i]-' 0 ') *4+ (str2[i+1]-' 0 ') *2+ (str2[i+2]-' 0 ');        ++j;            } for (I=0;i<j;++i) {if (i==0 && arr[i]==0) continue;        cout<<arr[i];            } cout<<endl; } return 0;}



A classmate of the Java AC Code: (Must learn!) )//Attached author URL: http://blog.csdn.net/u010887744/article/details/44889037
Import Java.io.bufferedreader;import java.io.ioexception;import Java.io.inputstreamreader;import Java.util.Scanner ;p Ublic class Main {public static void Main (string[] args) throws IOException {BufferedReader in = new Buffere        Dreader (New InputStreamReader (system.in));        int n = integer.parseint (In.readline ());        String a[] = new String[n];        for (int i = 0; i < n; i++) {A[i] = In.readline ();            } for (int i = 0; i < n; i++) {char[] temp = A[i].tochararray ();            StringBuffer s2 = new StringBuffer ();            2 int k = temp.length; for (int j = 0; J < K; J + +) {switch (Temp[j]) {case ' 0 ': S2.append ("                    0000 ");                Break                    Case ' 1 ': S2.append ("0001");                Break                    Case ' 2 ': S2.append ("0010");        Break        Case ' 3 ': S2.append ("0011");                Break                    Case ' 4 ': S2.append ("0100");                Break                    Case ' 5 ': S2.append ("0101");                Break                    Case ' 6 ': S2.append ("0110");                Break                    Case ' 7 ': S2.append ("0111");                Break                    Case ' 8 ': S2.append ("1000");                Break                    Case ' 9 ': S2.append ("1001");                Break                    Case ' A ': S2.append ("1010");                Break                    Case ' B ': s2.append ("1011");                Break                    Case ' C ': S2.append ("1100");                Break                    Case ' D ': S2.append ("1101");                Break    Case ' E ':                S2.append ("1110");                Break                    Case ' F ': s2.append ("1111");                Break            }}//2 to 8 stringbuffer s3 = new StringBuffer ();            int m = 0;                if (4 * k% 3 = = 1) {s3.append (s2.substring (0, 1));            M + = 1; } else if (4 * k% 3 = = 2) {switch (s2.substring (0, 2)) {case "S3":                    Append ("1");                Break                    Case "Ten": S3.append ("2");                Break                    Case "One": S3.append ("3");                Break                Default:break;            } m + = 2; } for (int j = m; J < 4 * k;)                    {Switch (S2.substring (J, J + 3)) {case ': S3.append ("0"); BreAk                    Case "001": S3.append ("1");                Break                    Case "010": S3.append ("2");                Break                    Case "011": S3.append ("3");                Break                    Case "S3.append": ("4");                Break                    Case "101": S3.append ("5");                Break                    Case "S3.append": ("6");                Break                    Case "111": S3.append ("7");                Break            } j + = 3; }//Delete 0//Use Delete (old is 0) or charAt if (s3.length () = = 2 && s3.charat            (0) = = ' 0 ') {//0-->00-->delete 00-->notany System.out.println (s3.substring (1));                } else {int q = 0;            while (S3.charat (q) = = ' 0 ') {        q++;                } String s = s3.tostring ();            System.out.println (s3.substring (q)); }        }    }}




Blue Bridge Cup-basic exercise-16 binary to octal (is timed out!!!) )

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.