从第M-1级楼梯上到第M级只有一种方法,即
a
n
=
a
n
−
1
+
C
a_n = a_{n-1}+C
an=an−1+C (C为第二种方法的数目)
从第M-2级楼梯上到第M级也只有一种方法(一级一级上属于第一种),即
a
n
=
a
n
−
1
+
a
n
−
2
a_n = a_{n-1}+a_{n-2}
an=an−1+an−2
/*
*
*Problem Description
*有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
*
*
*Input
*输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。
*
*
*Output
*对于每个测试实例,请输出不同走法的数量
*
*
*Sample Input
*2
*2
*3
*
*
*Sample Output
*1
*2
*
*
*Author
*lcy
*
*
*Source
*2005实验班短学期考试
*
*
*Recommend
*lcy
*
*/
#include<iostream>
using namespace std;
long long a[41] = { 0,0,1,2 };
void func() {
for (int i = 4; i < 41; i++) {
a[i] = a[i - 1] + a[i - 2];
}
}
int main() {
int n, x;
func();
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
cout << a[x] << endl;
}
system("pause");
return 0;
}