code, software architect, articles and novels. 代码,软件架构,博客和小说
用Python求解组合问题
Posted onEdited onWord count in article: 246Reading time ≈1 mins.
改动一下排列的代码,迅速变成组合问题的求解:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
defperm(n,m,current,lists=[]): if m == len(lists): return1 sum = 0 for i inrange(current,n+1): if i notin lists: sum+=perm(n,m,i+1,lists+[i]) returnsum
n,m = [int(i) for i in raw_input().split()] print (perm(n,m,1))