Chinaunix首页 | 论坛 | 博客
  • 博客访问: 240168
  • 博文数量: 47
  • 博客积分: 1229
  • 博客等级: 中尉
  • 技术积分: 568
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-20 10:06
文章分类

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-03-04 23:54:18

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?
--------------------
  1. #include

  2. #define M 20
  3. #define N 20


  4. long long cal(int m, int n)
  5. {
  6.     if (m == 0 || n==0) return 1;

  7.     return cal(m-1, n) + cal(m, n-1);
  8. }


  9. int main(int argc, const char *argv[])
  10. {

  11.     printf("%lld\n", cal(M, N));
  12.     
  13.     return 0;
  14. }

  15.     耗费了46m,应该有更快更好的办法。

  1. #include<stdio.h>

  2. #define N 20
  3. int main()
  4. {
  5.     long long result=1;
  6.     int i;

  7.     for (i=1;i<=N;i++) {
  8.         result = result*(N+i);
  9.         result /= i;

  10.     }

  11.     printf("result: %lld\n",result);

  12.     return 0;

  13. }
排列组合! perfect!
阅读(1236) | 评论(0) | 转发(0) |
0

上一篇:eule16

下一篇:awk删除svn空白日志

给主人留下些什么吧!~~