Re: [問題] 1-9位數不重複印出來(Python)
偷拿itertools來改,9位數跑完大約7~8秒,供參考
def creator(num):
ans = list()
pools = [x for x in range(1,10)]
for n in range(num):
result = [[]]
for i in range(n+1):
result = [x+[y] for x in result for y in pools if y not in x]
for res in result:
ans.append(sum([k * (10**j) for j, k in enumerate(res)]))
ans.sort()
print(*ans)
if __name__ == '__main__':
creator(int(input()))
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 39.9.168.141
※ 文章網址: https://www.ptt.cc/bbs/Programming/M.1494661305.A.036.html
Programming 近期熱門文章
PTT數位生活區 即時熱門文章