from scipy.special import comb n = int (input("n: ")) k = int (input("k: ")) sum1 = 0 for j in range(0, k): for i in range(0, n+1): sum1 = sum1 + (i**j)*comb(k+1,j) result = ((n+1)**(k+1) - sum1)/comb(k+1,k) print(result)
python 中计算排列组合:
1. 调用scipy计算
>> from scipy.special import comb, perm >> perm(3, 2) 6.0 >> comb(3, 2) 3.0
2. 调用 itertools 获取排列组合的全部情况数
>> from itertools import combinations, permutations >> permutations([1, 2, 3], 2) <itertools.permutations at 0x7febfd880fc0> # 可迭代对象 >> list(permutations([1, 2, 3], 2)) [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)] >> list(combinations([1, 2, 3], 2)) [(1, 2), (1, 3), (2, 3)]