Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207861
  • 博文数量: 87
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 798
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-14 14:54
文章分类

全部博文(87)

文章存档

2015年(87)

我的朋友

分类: C/C++

2015-08-23 12:55:32

腾讯2013笔试题:


首先我们研究从A到B有多少种走法,定义f(x,y),其中x为A到B横向走的格数,y为A到B纵向走的格数,则A到B有f(x,y)中走法,不难看出

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. f(x,y)=f(x-1,y)+f(x,y-1)  

因此f(x,y)问题可以用递归算法来做,其中

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 递归公式为:f(x,y)=f(x-1,y)+f(x,y-1)  
  2. 边界条件为:f(x,0)=f(0,y)=1  

而问题求的是不经过点p的路径数,A到B的路径数为f(7,5),A到P的路径数为f(3,3),P到B的路径数为f(4,2),因此解为f(7,5)-f(3,3)*f(4,2)

1.程序实现代码:
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int f(int x,int y)  
  5. {  
  6.     if(x==0||y==0)  
  7.         return 1;  
  8.     else  
  9.         return f(x,y-1)+f(x-1,y);  
  10. }  
  11. int main()  
  12. {  
  13.    cout<<f(7,5)-f(3,3)*f(4,2)<<endl;  
  14. }  
运行结果:
[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 492  

阅读(783) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~