BASIC-2 01 字串

BASIC-2 01 字串

题目

问题描述

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

输入格式

本试题没有输入。

输出格式

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

样例输出

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

题解

public class BASIC_2 {
    public static void main(String args[]) {
        for (int i = 0; i < 32; i++) {
            int[] arr = toTwo(i);
            for (int j = 0; j < 5; j++)
                System.out.print(arr[j]);
            System.out.println(" ");
        }
    }

    public static int[] toTwo(int num) {
        int[] arr = { 0, 0, 0, 0, 0 };
        for (int i = 4; num != 0; i--) {
            arr[i] = num % 2;
            num /= 2;
        }
        return arr;
    }
}
posted @ 2022-03-18 17:45  morning-start  阅读(35)  评论(0编辑  收藏  举报