标签:
实现组合算法C(n,k),可以用递归实现:
python代码:
1 import copy #实现list的深复制 2 3 def combine(lst, l): 4 result = [] 5 tmp = [0]*l 6 length = len(lst) 7 def next_num(li=0, ni=0): 8 if ni == l: 9 result.append(copy.copy(tmp)) 10 return 11 for lj in range(li,length): 12 tmp[ni] = lst[lj] 13 next_num(lj+1, ni+1) 14 next_num() 15 return result
标签:
原文地址:http://www.cnblogs.com/DaLiNDluDlu/p/5471881.html