求助大佬这个代码的问题
def cni(n,i):minNI = min(i , n-i)
result = 1
for j in range(0,minNI):
result = result * (n - j) / (nimNI - j)
return result
计算组合数Cn,但由于浮点数除法时精度问题导致错误,不知道怎么改 试试这个模块 decimal
参考这个 https://blog.csdn.net/haeasringnar/article/details/105471581?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163487950416780264069148%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=163487950416780264069148&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-105471581.first_rank_v2_pc_rank_v29&utm_term=python+%E6%B5%AE%E7%82%B9%E6%95%B0%E7%B2%BE%E5%BA%A6&spm=1018.2226.3001.4187 def f(n): # 阶乘 factorial
res = 1
for i in range(1, n+1): res *= i
return res
def C(r, n): # 组合数公式
return float(f(n))/float(f(r)*f(n-r))
print(C(3, 5))10.0 解决了,谢谢大佬们
页:
[1]