Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner.
How many routes are there through a 20×20 grid?
--------------------
-
#include
-
-
#define M 20
-
#define N 20
-
-
-
long long cal(int m, int n)
-
{
-
if (m == 0 || n==0) return 1;
-
-
return cal(m-1, n) + cal(m, n-1);
-
}
-
-
-
int main(int argc, const char *argv[])
-
{
-
-
printf("%lld\n", cal(M, N));
-
-
return 0;
-
}
-
-
耗费了46m,应该有更快更好的办法。
- #include<stdio.h>
-
-
#define N 20
-
int main()
-
{
-
long long result=1;
-
int i;
-
-
for (i=1;i<=N;i++) {
-
result = result*(N+i);
-
result /= i;
-
-
}
-
-
printf("result: %lld\n",result);
-
-
return 0;
-
-
}
排列组合! perfect!
阅读(1313) | 评论(0) | 转发(0) |