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

作者: dcerberus (豬豬毅)   2017-05-13 15:41:13
偷拿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()))

Links booklink

Contact Us: admin [ a t ] ucptt.com