超级台阶
来源:互联网 发布:肇庆学院网络自助中心 编辑:程序博客网 时间:2024/05/01 05:11
算法:搜索(打表)
题目描述
有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第m级,共有多少走法?
注:规定从一级到一级有0种走法。
输入
输入数据首先包含一个整数n(1<=n<=100),表示测试实例的个数,然后是n行数据,每行包含一个整数m,(1<=m<=40), 表示楼梯的级数。
输出
对于每个测试实例,请输出不同走法的数量。
样例输入
2
2
3
样例输出
1
2
AC代码: #include <iostream> using namespace std; int a[45]={0, 0 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34 ,55 ,89 ,144 ,233 ,377 ,610 ,987 ,1597 ,2584 ,4181 ,6765 ,10946 ,17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578 ,5702887 ,9227465 ,14930352 ,24157817 ,39088169 ,63245986 ,102334155}; int main() { int t; cin>>t; while(t--) { int n; cin>>n; cout<<a[n]<<endl; } return 0; }
打表代码:#include <iostream> #include <string> #include <cstring> #include <algorithm> #include <stdio.h> #include <iomanip> using namespace std; int a[45]; void dfs(int x) { if(x>40) return; for(int i=1;i<=2;i++) { x=x+i; a[x]++; dfs(x); x=x-i;} } int main() { dfs(1);int i,j,k,n,m,t;scanf("%d",&t); while(t--) { scanf("%d",&n); printf("%d\n",a[n]);}return 0; }
打表AC代码:
#include<iostream> using namespace std; int main() { int a[45]; a[0]=0;a[1]=0;a[2]=1;a[3]=2; for(int i=4;i<=40;i++) a[i]=a[i-1]+a[i-2];//规律 int t; cin>>t; while(t--) { int n; cin>>n; cout<<a[n]<<endl; } return 0; }
0 0
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- 超级台阶
- Playing Tricircle with Virtualbox
- Activity周期与Intent跳转活动的关系
- Quartz2D - 图片裁剪为正圆形
- hdu 1198 Farm Irrigation(并查集强大无比的位运算)
- ubuntu 14.04 "子进程 已安装 post-removal 脚本 被信号(已终止) 终止了“解决方法
- 超级台阶
- 欢迎使用CSDN-markdown编辑器
- 632A.Gradma Laura and Apples
- 珠链分割
- [Offer收割]编程练习赛1 hihocoder 1268 九宫 (DFS)
- nginx支持CI框架PATH_INFO模式
- 【小程序】日期类date的使用方法
- 5-32 说反话-加强版
- 机器学习-树类模型总结