用Python求解组合问题

改动一下排列的代码,迅速变成组合问题的求解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

def perm(n,m,current,lists=[]):
if m == len(lists):
return 1
sum = 0
for i in range(current,n+1):
if i not in lists:
sum+=perm(n,m,i+1,lists+[i])
return sum

n,m = [int(i) for i in raw_input().split()]

print (perm(n,m,1))


python 上手果真比较快…