1. 组合数公式: C(n, k) =C(n-1, k) +C(n-1, k-1) 要求利用该公式写递归函数求组合数。
#include <stdio.h> int C(int a,int b)
{
if(a==b||b==)
return ;
else
return C(a-,b)+C(a-,b-);
}
int main()
{
int n,k;
int s;
while(scanf("%d%d",&n,&k)!=EOF)
{
s=C(n,k);
printf("%d\n",s);
}
return ;
}
---------------------
原文:https://blog.csdn.net/rating_/article/details/84034728
# -*- coding:utf-8 -*-
# 用python实现排列组合C(n,m) = n!/m!*(n-m)!
def get_value(n):
if n==1:
return n
else:
return n * get_value(n-1) def gen_last_value(n,m):
first = get_value(n)
print("n:%s value:%s"%(n, first))
second = get_value(m)
print("n:%s value:%s"%(m, second))
third = get_value((n-m))
print("n:%s value:%s"%((n-m), third))
return first/(second * third) if __name__ == "__main__":
# C(12,5)
rest = gen_last_value(5,3)
print("value:", rest)
# 本方法的流程:(5,3),first=5*4*3*2*1;second=3*2*1;third=first/second
# -*- coding:utf-8 -*-
# 用python实现排列组合C(n,m) = n!/m!*(n-m)!
def get_value(n):
if n==b:
return n
else:
return n * get_value(n-1)
def get_value_m(m):
if m==1:
return m
else:
return m * get_value_m(m-1) def gen_last_value(n,m):
first = get_value(n)
print("n:%s value:%s"%(n, first))
second = get_value_m(m)
print("n:%s value:%s"%(m, second))
return first/second if __name__ == "__main__":
n = int(input('num n:'))
m = int(input('num m:'))
b = n-m+1
rest = gen_last_value(n,m)
print("value:", rest)
#举例:C(5,3);get_value(n)输出:5*4*3】【get_value_m输出:3*2*1】【gen_last_value=first/second
升级版