基础练习 01字串 java 题解 2

问题描述

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

00000

00001

00010

00011

00100

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

输入格式

本试题没有输入。

输出格式

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

样例输出

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

解题思路:

五层for循环,每层控制一位,最后格式化输出。

java代码:

public class Main {
	public static void main(String[] args) {
		for(int a = 0;a < 2;a++) {
			for(int b = 0;b < 2;b++) {
				for(int c = 0;c < 2;c++) {
					for(int d = 0;d < 2;d++) {
						for(int e = 0;e < 2;e++) {
							System.out.printf("%d%d%d%d%d\n",a,b,c,d,e);
						}
					}
				}
			}
		}
	}
}

提交截图:

おすすめ

転載: blog.csdn.net/weixin_48898946/article/details/120808292