基础练习 01字串

来源:互联网 发布:2017年播的网络腐剧 编辑:程序博客网 时间:2024/06/10 11:51
 基础练习 01字串  
时间限制:1.0s   内存限制:256.0MB
      
问题描述

对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:

00000

00001

00010

00011

00100

请按从小到大的顺序输出这32种01串。

输入格式
本试题没有输入。
输出格式
输出32行,按从小到大的顺序每行一个长度为5的01串。
样例输出
00000
00001
00010
00011
<以下部分省略>

方法一:

源代码

1
public class Main {  
2
  
3
    /** 
4
     * @param args 
5
     */  
6
    public static void main(String[] args) {  
7
        // TODO Auto-generated method stub;  
8
        for (int i = 0; i <= 1; i++)  
9
            for (int j = 0; j <= 1; j++)  
10
                for (int k = 0; k <= 1; k++)  
11
                    for (int a = 0; a <= 1; a++)  
12
                        for (int b = 0; b <= 1; b++) {  
13
                            System.out.println(i + "" + j + "" + k + "" + a  
14
                                    + "" + b);  
15
  
16
                        }  
17
    }  
18
}  
方法二:(注意不要有包名)

源代码

1
public class Main {
2
public static void main(String[] args){
3
  for(int i=0;i<32;i++)
4
  {System.out.print(i%32/16);
5
  System.out.print(i%16/8);
6
  System.out.print(i%8/4);
7
  System.out.print(i%4/2);
8
  System.out.print(i%2);
9
  System.out.println();
10
  }}
11
}
12



0 0
原创粉丝点击