AcWing 94. 【python】递归实现排列型枚举
原题链接
简单
作者:
tt2767
,
2019-12-11 00:04:55
,
所有人可见
,
阅读 899
import sys
n = int(sys.stdin.read())
# def dfs(nums, buff):
# if len(buff) == n:
# print ' '.join(map(str, buff))
# else:
# for i in range(len(nums)):
# buff.append(nums[i])
# dfs(nums[0:i] + nums[i+1:], buff)
# buff.pop()
# dfs(range(1, n+1), [])
def dfs(status, buff):
if len(buff) == n:
print ' '.join(map(lambda x: str(x+1), buff))
else:
for i in range(n):
if status >> i & 1 == 0:
buff.append(i)
dfs(status | 1 << i, buff)
buff.pop()
dfs(0, [])