sicily——Up Stairs
2016-02-21 19:45
573 查看
Description
小明在走楼梯,他每步可以跨一级、二级或者三级楼梯。
请问从平地开始,走到第n(1 <= n <= 30)级楼梯,有多少种走法?
Input
第一行为一个整数m,表示测试用例个数。
接下来有m行,每一行包含一个正整数n。
Output
对于每一个正整数n,输出走法数目,每个输出值占一行。
Sample Input
Copy sample input to clipboard
Sample Output
Hint
走到第3级楼梯的方法有4种:
1. 第一步走到第1级,第二步走到第2级,第三步走到第3级
2. 第一步走到第1级,第二步走到第3级
3. 第一步走到第2级,第二步走到第3级
4. 第一步走到第3级
一个递归程序的应用,也是一个dfs算法的应用,开始想的太复杂,现在秒懂!
小明在走楼梯,他每步可以跨一级、二级或者三级楼梯。
请问从平地开始,走到第n(1 <= n <= 30)级楼梯,有多少种走法?
Input
第一行为一个整数m,表示测试用例个数。
接下来有m行,每一行包含一个正整数n。
Output
对于每一个正整数n,输出走法数目,每个输出值占一行。
Sample Input
Copy sample input to clipboard
3 3 4 5
Sample Output
4 7 13
Hint
走到第3级楼梯的方法有4种:
1. 第一步走到第1级,第二步走到第2级,第三步走到第3级
2. 第一步走到第1级,第二步走到第3级
3. 第一步走到第2级,第二步走到第3级
4. 第一步走到第3级
一个递归程序的应用,也是一个dfs算法的应用,开始想的太复杂,现在秒懂!
#include<iostream> #include<iomanip> #include<queue> #include<cmath> using namespace std; int n,cnt; int num[3]={1,2,3}; void dfs(int a) { if(a>=n)//它已经走到n级或者超过n级 { if(a==n)//刚好n级时就计数 cnt++; return; } else { for(int i=0;i<3;i++)//每一级都尝试下一步的所有可能情况 { dfs(a+num[i]); } } return; } int main() { int t; cin>>t; while(t--) { cnt=0; cin>>n; dfs(0); cout<<cnt<<endl; } return 0; }哈哈,明天开学,加油,新的征程!
相关文章推荐
- Scala的trait
- B - A + B Again
- lintcode-easy-Climbing Stairs
- 可以填空的TextView
- Comparison of video container formats
- Aizu2130-Billion Million Thousand-dp
- raid以及mdadm命令之二
- http://blog.csdn.net/antjumper/article/details/39479187
- Machine Learning - Regularized Logistic Regression
- 2016-02-21-阅读笔记:大脑计划+3rd open GPU+ Delip质疑google Swivel中loss-function + training trick BN in DL
- Unable to simultaneously satisfy constraints.这一问题
- Main方法格式讲解
- main 函数
- reverse mapping checking getaddrinfo for bogon failed – POSSIBLE BREAK-IN ATTEMPT! 错误
- scala中的trait
- vector::clear()和vecotr::pop_back()对内存处理的不同 http://blog.csdn.net/vieri_ch/article/details/1191390
- 关于破解AI的补丁和方法
- hdu 1151 Air Raid(最小路径覆盖)
- CodeForces 630 A. Again Twenty Five!(水~)
- git@xxxx.git: Auth fail错误