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