Re: [問題] 1-9位數不重複印出來 (Python)

看板Programming作者 (企鵝)時間7年前 (2016/12/03 00:15), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/6 (看更多)
認真做 dfs, 結果是照字典序排 這個比較快的版本是直接改狀態 benchmark N = 8, 前篇 python3 用 len(set(x)) = len(x) 要 100 秒 這份大概五秒 XD #!/usr/bin/env python3 import sys def solve(N): used = [False] * 10 strs = [] def dfs(): yield ''.join(strs) if len(strs) == N: return for i in range(10): if not used[i]: strs.append(str(i)) used[i] = True yield from dfs() strs.pop() used[i] = False for i in range(1, 10): used[i] = True strs.append(str(i)) yield from dfs() used[i] = False strs.pop() power = int(sys.argv[1]) print(', '.join(solve(power))) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 158.222.146.102 ※ 文章網址: https://www.ptt.cc/bbs/Programming/M.1480695338.A.18C.html
文章代碼(AID): #1OGPug6C (Programming)
討論串 (同標題文章)
文章代碼(AID): #1OGPug6C (Programming)