指数型枚举
从 [ 1 , n ] [1, n] [1,n] 内这 n n n 个整数可以随机选取任意多个,输出所有可能的选择方案。
非递归实现
import sys
from itertools import accumulate, permutations, combinations
input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")
def read():
line = sys.stdin.readline().strip()
while not line:
line = sys.stdin.readline().strip()
return map(int, line.split())
def I():
return int(input())
n = I()
print()
for i in range(1, 1 << n):
tmp = i
# print(str(bin(tmp))[2::].zfill(n))
for j in range(n):
if (tmp >> j) & 1 == 1: # 如果i状态下的第j位为1
print(j + 1, end=' ')
print()
递归实现
升序
import sys
from itertools import accumulate, permutations, combinations
input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")
def read():
line = sys.stdin.readline().strip()
while not line:
line = sys.stdin.readline().strip()
return map(int, line.split())
def I():
return int(input())
n = I()
chosen = []
def calc(x):
if x == n + 1:
print(*chosen)
return
# 不选取x
calc(x + 1)
# 选取x
chosen.append(x)
calc(x + 1)
chosen.pop()
calc(n)
降序
import sys
from itertools import accumulate, permutations, combinations
input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")
def read():
line = sys.stdin.readline().strip()
while not line:
line = sys.stdin.readline().strip()
return map(int, line.split())
def I():
return int(input())
n = I()
chosen = []
def calc(x):
if x == 0:
print(*chosen)
return
# 不选取x
calc(x - 1)
# 选取x
chosen.append(x)
calc(x - 1)
chosen.pop()
calc(n)
组合型枚举
从 [ 1 , n ] [1, n] [1,n] 内这 n n n 个整数可以随机选取 m m m 个,输出所有可能的选择方案。
非递归实现
import sys
from itertools import accumulate, permutations, combinations
input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")
def read():
line = sys.stdin.readline().strip()
while not line:
line = sys.stdin.readline().strip()
return map(int, line.split())
def I():
return int(input())
n, m = read()
for t in combinations(range(1, n + 1), m):
print(*t)
递归实现
import sys
from itertools import accumulate, permutations, combinations
input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")
def read():
line = sys.stdin.readline().strip()
while not line:
line = sys.stdin.readline().strip()
return map(int, line.split())
def I():
return int(input())
n, m = read()
chosen = []
def calc(x):
if len(chosen) > m or len(chosen) + (n - x + 1) < m:
return
if x == n + 1:
print(*chosen)
return
# 选取x
chosen.append(x)
calc(x + 1)
chosen.pop()
# 不选取x
calc(x + 1)
calc(1)
排列型枚举
把 1 1 1 ~ n n n 这 n n n 个整数排成一行后随机打乱顺序,输出所有可能的次序。
非递归实现
import sys
from itertools import accumulate, permutations, combinations
input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")
def read():
line = sys.stdin.readline().strip()
while not line:
line = sys.stdin.readline().strip()
return map(int, line.split())
def I():
return int(input())
n = I()
for t in permutations(range(1, n + 1)):
print(*t)
递归实现
import sys
from itertools import accumulate, permutations, combinations
input = lambda: sys.stdin.readline().rstrip("\r\n")
printf = lambda d: sys.stdout.write(str(d) + "\n")
def read():
line = sys.stdin.readline().strip()
while not line:
line = sys.stdin.readline().strip()
return map(int, line.split())
def I():
return int(input())
n = I()
chosen = [0] * (n + 1) # 表示第i位数字是否被选取
order = [0] * (n + 1) # 表示选取的第k位数字
def calc(k):
# 选取第k个数字
if k == n + 1:
print(*order[1::])
return
for i in range(1, n + 1):
if chosen[i] == 0:
order[k] = i
chosen[i] = 1
calc(k + 1)
order[k] = 0
chosen[i] = 0
calc(1)