天天看點

藍橋杯練習系統之基礎練習 BASIC-2 01字串BASIC-2 01字串

BASIC-2 01字串

基礎練習 01字串 時間限制:1.0s 記憶體限制:256.0MB

問題描述

對于長度為5位的一個01串,每一位都可能是0或1,一共有32種可能。它們的前幾個是:

00000

00001

00010

00011

00100

請按從小到大的順序輸出這32種01串。

輸入格式

本試題沒有輸入。

輸出格式

輸出32行,按從小到大的順序每行一個長度為5的01串。

樣例輸出

00000

00001

00010

00011

<以下部分省略>

方法一:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		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);
							
						}
	}

}
           

方法二:

public class Main {

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