Blue Bridge Cup hex to octal (extra large test data, Java implementation)

Source: Internet
Author: User

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

Get the topic feel quite simple, directly using a string and integer several functions, sample output no problem. Submit to find prompt run error.

Download down the test data before you know what a perverted number the input is in:

1 Importjava.util.Arrays;2 ImportJava.util.Scanner;3 4  Public classMain {5     6      Public Static voidMain (string[] args) {7Scanner in=NewScanner (system.in);8         intn=in.nextint ();9String [] result=NewString[11];Ten          for(inti=0;i<n;i++){ OneString h=In.next (); AString b=integer.tobinarystring (integer.valueof (H, 16)); -String o=integer.tooctalstring (integer.valueof (b, 2)); -result[i]=o; the         } -          for(inti=0;i<n;i++){ - System.out.println (Result[i]); -         } +     } - } +     
code that was not passed for the first time

Test data:

Changes were made to the code for the first time:

① first converts each hexadecimal number to a four-bit binary number (must be converted to 4 bits, using leading 0), and saved as a string;

② the string length to a multiple of 3 to convert to octal;

③ every three bits into octal, minus the leading 0;

The code can be tested as follows:

1 Importjava.util.Arrays;2 ImportJava.util.Scanner;3 4  Public classPoj {5     6      Public Static voidMain (string[] args) {7Scanner in=NewScanner (system.in);8         intn=in.nextint ();9String [] result=NewSTRING[11];//Save the results of the last outputTen         /*hexadecimal conversion to binary binary*/ One          for(inti=0;i<n;i++){ AString h=In.next (); -StringBuilder tempb=NewStringBuilder (); -              for(intM=0;m){ the                 CharNumh=H.charat (m); -String b=integer.tobinarystring (integer.valueof (string.valueof (NUMH), 16)); -              -                  for(intK=b.length (); k<4;k++){ +b= ' 0 ' +b; -                 } +  A Tempb.append (b); at             } -              -             /*Binary into octal*/ -StringBuilder tempo=NewStringBuilder ();//The length becomes a multiple of 3 and the number of leading 0 that needs to be mended -             intAddzero=3-tempb.length ()%3; -              for(intp=0;p<addzero;p++){ intempb=NewStringBuilder ("0"). Append (TEMPB); -             } to              for(intM=0;m<tempb.length (); m+=3){ +                 //converts a string length to a multiple of three to add a leading 0 -String numb=tempb.substring (M, m+3); theString o=integer.tooctalstring (integer.valueof (string.valueof (NumB), 2)); * tempo.append (o); $             }Panax NotoginsengResult[i]=tempo.tostring (). ReplaceAll ("^ (0+)", "" ");//Remove leading 0 with regular expressions -         } the          for(inti=0;i<n;i++){ + System.out.println (Result[i]); A         } the     } +}

Blue Bridge Cup hex to octal (extra large test data, Java implementation)

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.