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

来源:互联网 发布:中山广电网络客服电话 编辑:程序博客网 时间:2024/05/22 05:24

 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;         }        }