蓝桥杯练习系统之基础训练Java版(2-2)——01字串问题

蓝桥杯练习系统之基础训练Java版(2-2)——01字串问题

**难度等级:易**

**关键字:循环**

资源限制
时间限制:1.0s 内存限制:256.0MB

问题描述:

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

00000

00001

00010

00011

00100

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

输入格式:

本试题没有输入。

输出格式:

输出32行,按从小到大的顺序每行一个长度为5的01串。

样例输出:

00000
00001
00010
00011
<以下部分省略>

暴力破解法(其他文章有提及到,笔者没在学习系统提交过,不清楚根据样例输出直接打印出来是否可以,所以不做介绍。)

利用for循环(最符合竞赛要求)

public class Main {

	public static void main(String[] args) {
		
		int a, b, c, d, e;
		
		for(e = 0; e <= 1; e++) 
		for(d = 0; d <= 1; d++) 
		for(c = 0; c <= 1; c++) 
		for(b = 0; b <= 1; b++) 
		for(a = 0; a <= 1; a++) {
			
			System.out.print(e);
			System.out.print(d);
			System.out.print(c);
			System.out.print(b);
			System.out.println(a);
		}		
			
	}
}

希望能对您有帮助!谢谢。(新手入门,写得不好的,可以提建议哦,希望一起共同进步!)

你可能感兴趣的:(蓝桥杯)