LoginSignup
0
0

More than 3 years have passed since last update.

メモ:n桁の2進数の数をまとめて作成

Last updated at Posted at 2019-10-06

プログラミングコンテストで使った処理。
実行環境は、Python3.4。

プログラム1

N = 3  # 桁数

for i in range(2 ** N):
    x = format(i, '0{}b'.format(N))
    print(x)

# 実行結果
# 000
# 001
# 010
# 011
# 100
# 101
# 110
# 111

プログラム2

for i in range(2):
    for j in range(2):
        for k in range(2):
            s = str(i) + str(j) + str(k)
            print(s)

# 実行結果
# 000
# 001
# 010
# 011
# 100
# 101
# 110
# 111

もっといいやり方がありそう。

0
0
2

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