Blue Bridge Cup Basics Practice Java 01-string-in-a-binary conversion

Source: Internet
Author: User
Tags binary to decimal

Problem Description

For a 01-string length of 5 bits, each bit can be 0 or 1, a total of 32 possible. The first few of them are:

00000

00001

00010

00011

00100

Please output these 32 kinds of 01 strings in order from small to large.

Input FormatThis question is not entered. output Formatoutput 32 rows, in order from small to large, each line is a length of 5 01 strings. Sample Output00000
00001
00010
00011
< The following sections omit >My idea: Define a string array from 0 to 31,for loop int values from 0 to 31, convert int to 2, get string, then judge the length of the string, not enough 5 bits in front of 0, and the last output stringcomplementary points of knowledge: conversions between binaries   decimal to 16 binary: integer.tohexstring (int i)decimal into octal: integer.tooctalstring (int i)decimal goes to binary: integer.tobinarystring (int i)

hexadecimal to decimal: integer.valueof ("FFFF", +). ToString ()

Octal into decimal: integer.valueof ("876", 8). ToString ()

Binary to decimal: integer.valueof ("0101", 2). ToString ()

1  Public class_201 Sort {2     Public Static voidMain (string[] args) {3String[] Strings =NewString[32];4       for(inti = 0; i < strings.length; i++) {5Strings[i] = integer.tobinarystring (i);//decimal conversion to 2 binary6         //0 in front of numbers not enough 5 digits7         if(strings[i].length () = = 1) {//1-digit number8Strings[i] = "0000" +Strings[i];9}Else if(Strings[i].length () ==2) {TenStrings[i] = "000" +Strings[i]; One}Else if(Strings[i].length () ==3) { AStrings[i] = "00" +Strings[i]; -}Else if(Strings[i].length () ==4) { -Strings[i] = "0" +Strings[i]; the}Else{ -Strings[i] =Strings[i]; -         } -     } +       for(inti = 0; i < strings.length; i++) { - System.out.println (Strings[i]); +     } A } at}

Blue Bridge Cup Basics Practice Java 01-string-in-a-binary conversion

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.