本文介绍了python中有数学nCr函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我期待看看是否内置了python中的数学库是nCr(n选择r)函数:
据我所知,这可以编程,但我认为我会在做之前检查它是否已经内置。
解决方案
以下程序以有效的方式计算 nCr
计算阶乘因子等)
pre $ $ $ $ $ $ $导入运算符为
def ncr(n,r)
r = min(r,nr)
numer = reduce(op.mul,xrange(n,nr,-1),1)
denom = reduce(op.mul,xrange(1,r + 1) ,1)
return numer // denom
I'm looking to see if built in with the math library in python is the nCr (n Choose r) function:
I understand that this can be programmed but I thought that I'd check to see if it's already built in before I do.
解决方案
The following program calculates nCr
in an efficient manner (compared to calculating factorials etc.)
import operator as op
def ncr(n, r):
r = min(r, n-r)
numer = reduce(op.mul, xrange(n, n-r, -1), 1)
denom = reduce(op.mul, xrange(1, r+1), 1)
return numer//denom
这篇关于python中有数学nCr函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!