Algorithm training 6-2 Recursive binary representation of digit time limit: 10.0s memory Limit: 256.0MB problem description Given a decimal integer, returns the number of bits of its corresponding binary number. For example, enter decimal number 9, which corresponds to a binary number of 1001, so the number of bits is 4. Sample input An input example that satisfies the requirements of the topic.
The output of the 9 sample output corresponds to the example input above.
Data size and convention the range of each number in the input data.
Example: The input is within the range of int representation.
Topic Analysis:
In the method of the Integer class of Java, there are three methods that convert decimal numbers to binary, octal, and hexadecimal. If the argument is a positive integer, the string representation of an integer argument is returned as an X-signed integer, otherwise the unsigned integer value is the parameter plus 2^32.
in the subject, call the Tobinarystring () method of the Integer class directly, return the string and then call the length () method to obtain the binary representation of the number of bits.
added: Decimal to 16 binary: integer.tohexstring (int i)
decimal Turn octal: integer.tooctalstring (int i)
decimal Turn binary: integer.tobinarystring (int i)
hexadecimal to decimal: integer.valueof ("FFFF", +). ToString ()
Octal decimal: integer.valueof ("123", 8). ToString ()
Binary to decimal: integer.valueof ("1001", 2). ToString ()
Example code:
1 ImportJava.util.Scanner;2 3 Public classMain {4 Public Static voidMain (string[] args) {5Scanner sc =NewScanner (system.in);6 intn =sc.nextint ();7 8 //because the string representation of an integer argument is returned, the length () method is used to obtain the string lengths9 intLen =integer.tobinarystring (n). Length ();Ten One System.out.println (len); A } -}
Blue Bridge Cup algorithm training ALGO-151 6-2 recursive binary representation of digits