基础练习 01字串(ACMORE1559)

基础练习 01字串(ACMORE1559)

题目

Description
对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能。它们的前几个是:
00000
00001
00010
00011
00100
请按从小到大的顺序输出这32种01串。
Input
本试题没有输入。
Output
输出32行,按从小到大的顺序每行一个长度为5的01串。
Sample Input

Sample Output
00000
00001
00010
00011
Source
蓝桥杯
题目链接: https://acmore.cc/problem/LOCAL/1559

简析

题目很简单,没什么可说的,但是,代码能否简洁呢?︿( ̄︶ ̄)︿
String.format("%5s", Integer.toBinaryString(i)).replace(" ", "0")
先看String.format("%5s", Integer.toBinaryString(i)),这样格式化出来的是用空格补齐的,然后再用replace(" ", "0")将空格替换为0

AC的代码

public class Main {
	public static void main(String[] args) {
		for(int i = 0; i < 32; i++) {
			System.out.println(String.format("%5s", Integer.toBinaryString(i)).replace(" ", "0"));
		}
	}
}
发布了118 篇原创文章 · 获赞 479 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/y_universe/article/details/86551842