蓝桥杯 基础练习 BASIC-2 01字串

基础练习 01字串

  1. 问题描述

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

00000

00001

00010

00011

00100

请按从小到大的顺序输出这32种01串。
输入格式
本试题没有输入。
输出格式
输出32行,按从小到大的顺序每行一个长度为5的01串。
样例输出
00000
00001
00010
00011
<以下部分省略>
2.思路:
主要使用了Java的一个方法Integer.toBinaryString()
3.主要代码如下


public class BASIC_2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//int n=5;
		for(int i=0;i<Math.pow(2,5);i++){
			if(Integer.toBinaryString(i).length()<5){
				int l=5-Integer.toBinaryString(i).length();
				String str = "0";
				for(int j=1;j<l;j++){
					str+="0";
				}
				System.out.println(str+Integer.toBinaryString(i));
			}
			else{
				System.out.println(Integer.toBinaryString(i));
			}
		}
		

	}

}
发布了8 篇原创文章 · 获赞 0 · 访问量 227

猜你喜欢

转载自blog.csdn.net/LilyS1/article/details/103964977