您的位置:首页 > 其它

poj 1942 Paths on a Grid 求组合数mCn的方法

2013-09-14 10:59 381 查看
unsigned int comb(unsigned a,unsigned b)

{

if(b > a-b)

b= a-b;

double result = 1;

while(b>0)

{

result *= (double)a / (double) b;

//cout<<"result:"<<result<<endl;

--a;

--b;

}

return ( unsigned int)(result + 0.5); //四舍五入

}

一个求组合数的函数

#include <iostream>
#include <stdio.h>

using namespace std;

//C(a,b)
//
unsigned int comb(unsigned  a,unsigned  b)
{
if(b > a-b)
b= a-b;

double result = 1;

while(b>0)
{
result *= (double)a / (double) b;

//cout<<"result:"<<result<<endl;
--a;
--b;
}

return ( unsigned int)(result + 0.5);
}

int main()
{
unsigned int m,n;
while(cin>>m>>n)
{
if(m==0&&n==0)
break;

cout<<comb(m+n,m)<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: