0
0

More than 3 years have passed since last update.

Javaでbit全探索するときのいつもの形。

Posted at

そもそもbit全探索って何

ぐぐると山ほど見つかります。。。

自作のコード(Java)

public class Main {

    public static void main(String[] args) {

        // 「2の5乗」回ループ
        for (int i = 0; i < (1 << 5); i++) {

            // 5回ループ
            for (int j = 0; j < 5; j++) {
                if ((1 & i >> j) == 1) {
                    System.out.print("o");
                } else {
                    System.out.print("x");
                }
            }

            System.out.println(" " + i);
        }

    }

}

上記コードの実行結果

xxxxx 0
oxxxx 1
xoxxx 2
ooxxx 3
xxoxx 4
oxoxx 5
xooxx 6
oooxx 7
xxxox 8
oxxox 9
xoxox 10
ooxox 11
xxoox 12
oxoox 13
xooox 14
oooox 15
xxxxo 16
oxxxo 17
xoxxo 18
ooxxo 19
xxoxo 20
oxoxo 21
xooxo 22
oooxo 23
xxxoo 24
oxxoo 25
xoxoo 26
ooxoo 27
xxooo 28
oxooo 29
xoooo 30
ooooo 31

おしまい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0