poj1942求组合数

来源:互联网 发布:算法统宗校释 编辑:程序博客网 时间:2024/04/30 03:05

就是一求组合数的问题。


#include <iostream>#include <algorithm>using namespace std;#define ULL unsigned long long intint main(){    ULL a, b;    while (cin >> a >> b && (a || b))    {        if (b > a) swap(a, b);        ULL s = 1, i, j;        for (i = a + 1, j = 1; i <= a + b; i++, j++)        {            s = s * i / j;            cout<<i<<" "<<j<<" "<<s<<endl;        }        cout << s << endl;    }}



或者

/*强制类型转换输出*///Memory Time //220K   0MS #include<iostream>#include<math.h>using namespace std;/*Compute (n+m)C min{n,m}*/unsigned comp(unsigned n,unsigned m){unsigned a=m+n;unsigned b=(m<n?m:n);double cnm=1.0;while(b>0)cnm*=(double)(a--)/(double)(b--);    cnm+=0.5;      //double转unsigned会强制截断小数,必须先四舍五入return (unsigned)cnm;}int main(void){unsigned m,n;while(true){cin>>m>>n;if(!m && !n)//承认这题的猥琐吧!竟然有其中一边为0的矩阵,一定要&&,用||会WAbreak;cout<<comp(n,m)<<endl;}return 0;}


0 0
原创粉丝点击