计算组合数
#include <iostream>
using namespace std;
//int Goup_num(int a,int b);
int Goup_num(int a,int b)
{
if(a<0||b<0||a<b) return 0; //排除不符合算法的情况
if(a==b) return 1;
if(b==1) return a;
else return Goup_num(a-1,b)+Goup_num(a-1,b-1);
}
int main()
{
cout<<Goup_num(6,2)<<endl; //测试数据
cout<<Goup_num(5,3)<<endl;
cout<<Goup_num(6,1)<<endl;
cout<<Goup_num(2,3)<<endl;
cout<<Goup_num(3,3)<<endl;
system("pause");
return 0;
}
核心公式 :C(a,b)=C(a-1,b)+C(a-1,b-1);