hdu2041-超级楼梯

超级楼梯

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 71790    Accepted Submission(s): 36599


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实验班短学期考试

题解:设f[i]为到达i级楼梯的方法数,因为每次只能晚上走一格或者走两格,故要到达i级只能从 i - 1级走一格上来或者从i - 2级走两格上来,因此f[i] = f[i - 1] + f[i - 2];

AC代码

#include <stdio.h>
#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <cmath>
 
using namespace std;

const int maxn = 44;
int f[maxn];

int main(){
	f[1] = 1;
	f[2] = 1;
	for(int i = 3; i <= 40; i++)
		f[i] = f[i - 1] + f[i - 2];
	int n, m;
	scanf("%d", &n);
	while(n--){
		scanf("%d", &m);
		printf("%d\n", f[m]);
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_37064135/article/details/80112502