Blue Bridge Cup algorithm training ALGO-151 6-2 recursive binary representation of digits

Source: Internet
Author: User
Tags binary to decimal

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

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.