超级台阶

算法:搜索(打表)

题目描述
有一楼梯共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;
  } 
 




posted @ 2016-03-07 19:51  (慎独)  阅读(343)  评论(0编辑  收藏  举报