|   Problem Description 有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? 
 Input 输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。 
 Output 对于每个测试实例,请输出不同走法的数量 
 Sample Input 2 2 3 
 Sample Output 1 2 
 Author lcy  |     
反过来想,每次只能走一级或两级阶梯,除了前两级外,想要到达这一级,肯定是从前一级或前两级走过来的
即 a[ i ] = a[ i -1] +a[ i -2] (斐波那契数列)
#include<iostream>
using namespace std;
typedef long long ll;
ll a[50];
void table(){
	a[1]=1;
	a[2]=1;         
	for(int i=3;i<=45;i++){
		a[i]=a[i-1]+a[i-2];
	}
}
int main(){
	int t,n;
	table();
	cin>>t;
	while(t--){
		cin>>n;
		cout<<a[n]<<endl;
	}
	return 0;
}   

京公网安备 11010502036488号