问题描述
对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:
00000
00001
00010
00011
00100
请按从小到大的顺序输出这32种01串。
输入格式
本试题没有输入。
输出格式
输出32行,按从小到大的顺序每行一个长度为5的01串。
样例输出
00000
00001
00010
00011
<以下部分省略>
00001
00010
00011
<以下部分省略>
方法1:
public class String01_2 { public static void main(String[] args) { for (int i = 0; i < Math.pow(2, 5); i++) { String s=Integer.toBinaryString(i).toString(); int l=5-s.length(); for (int j = 0; j < l; j++) { s="0"+s; } System.out.println(s); } } }
方法2:
public class String01 { public static void main(String[] args) { int[]a=new int[5]; for (int i = 0; i < Math.pow(2, 5); i++) { for (int j = 0; j < 4; j++) { if(a[j]==2){ a[j]=0; a[j+1]++; } else if(a[j]==1||a[j]==0){ break; } } for (int j = a.length-1; j >=0 ; j--) { System.out.print(a[j]); } a[0]++; System.out.println(); } } }