Welcome, guest | Sign In | My Account | Store | Cart

Python, 67 lines
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75``` ```#coding:utf-8 #全排列发生器 def a(n): li=[] for i in range(1,n+1): li.append(i) return li def c(i,li_a): li=[] for j in li_a: if j!=i: li.append(j) return li def form(i,li_c,one): li=[] li.append(i) count=0 for i in one: count=count+1 li.insert(count,li_c[i-1]) return li def fun(n): if n==2: return [[1,2],[2,1]] else: back=[]#返回集合 li_a=a(n) for i in li_a:#依次取li_a中的每一个 li_c=c(i,li_a)#生成li_a中除去i的补集 for one in fun(n-1):#取下一级的所有排列序 #li=函数(第一位i,补集li_c,序数列表one) li=form(i,li_c,one) back.append(li) return back li=fun(5) ##dic={1:"圣",2:"诞",3:"节",4:"快乐"} ##for li_in in li: ## word="" ## for i in li_in: ## word=dic[i]+word ## word="预祝大家:"+word ## print word count=1 for i in li: print str(count)+":"+str(i) count=count+1 ```

1 comment

David Lambert 13 years, 9 months ago

for a in itertools.permutations(range(1,6)): print(a)

(python3 equivalent of code)

 Created by nillgump nillgump on Sun, 22 Feb 2009 (MIT)

Required Modules

• (none specified)