二进制输出

来源:互联网 发布:python怎么往矩阵输入 编辑:程序博客网 时间:2024/05/19 02:23

对于长度为5的一个01串,每一位可能是0或1,一共有32种可能。它们的前几个是:
00000
00001
00010
00011
00100
00101
请按从小到大的顺序输出这32种01串。

代码如下

public class C1 {    public static void main(String[] args) {        for(int i=0;i<32;i++) {            String temp=Integer.toBinaryString(i);            while(temp.length()<5) {                temp=0+temp;            }            System.out.println(temp);        }    }}
原创粉丝点击