poj 1924 Paths on a Grid(组合数学)

题目:http://poj.org/problem?id=1942

题意:给定一个矩形网格的长m和高n,其中m和n都是unsigned int32类型,一格代表一个单位,就是一步,求从左下角到右上角有多少种走法,每步只能向上或者向右走

题解:就是 上和 右的排列。

用c(m,n)=n!/m!*(n-m)!;

最重要的是算阶乘。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 
 6 unsigned comb(unsigned m,unsigned n)
 7 {
 8     unsigned a,b;
 9     double cnt=1.0;
10     a=m+n;
11     b=(m>n?n:m);//从m n中选一个较小的
12     while(b)
13     cnt*=double(a--)/double(b--);//一种求c(b,a)的比较快的方法。
14     cnt+=0.5;//最后结果要四舍五入,所以要加0.5
15     return (unsigned)cnt;
16 }
17 int main()
18 {
19     unsigned n,m;//必须用unsigned,如果用int 会wa.
20     while(cin>>m>>n&&(n||m))
21         cout<<comb(m,n)<<endl;
22     return 0;
23 }

 

posted @ 2013-10-30 20:30  水门  阅读(162)  评论(0编辑  收藏  举报