POJ-1942 Paths on a Grid

题目链接:点击打开链接

题意:

给你一个方格, 求从左下角到右上角的路径数目。

思路:

每一步要么向上走要么向下走, 走的步数是一定的, 所以只需要求哪几步是向上走的情况就可以了, 即C(m,n).

代码:

#include <iostream>
#include <math.h>
using namespace std;

unsigned comp(unsigned n, unsigned m)
{
	unsigned a = m+n;
	unsigned b = min(m,n);
	double cnm = 1.0;
	while(b > 0)
		cnm *= (double)(a--)/(double)(b--);
	cnm += 0.5;
	return (unsigned)cnm;
}

int main()
{
	unsigned m, n;
	while(cin>>m>>n, m||n)
		cout<<comp(n,m)<<endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/JOKER_SAMA/article/details/52263399