题目 设计一个程序,计算组合数:C(m,r)=m!/(r!*(m-r)!),其中m,r为正整数,且m>r.分别求出C(4,2),C(6,4),C(8,7)的组合数.求阶乘和组合数分别用函数来实现.
时间: 2024-05-05 10:22:53 浏览: 42
计算组合数,用C语言编程
5星 · 资源好评率100%
实现如下:
```python
def factorial(n):
"""
计算阶乘
"""
if n == 0:
return 1
else:
return n * factorial(n-1)
def combination(m, r):
"""
计算组合数
"""
return factorial(m) // (factorial(r) * factorial(m-r))
# 求 C(4,2)
c1 = combination(4, 2)
print("C(4,2) = ", c1)
# 求 C(6,4)
c2 = combination(6, 4)
print("C(6,4) = ", c2)
# 求 C(8,7)
c3 = combination(8, 7)
print("C(8,7) = ", c3)
```
输出结果:
```
C(4,2) = 6
C(6,4) = 15
C(8,7) = 8
```
阅读全文