zufeoj_走台阶

题目链接:http://acm.ocrosoft.com/problem.php?cid=1172&pid=6

题目描述

一架楼梯共有n个台阶,上楼可以一步上1个台阶,也可以一步上2个台阶。问一共有多少种上楼的方法?

例如,当n = 5时,一共有8种方法:

5 = 1 + 1 + 1 + 1 + 1

5 = 2 + 1 + 1 + 1

5 = 1 + 2 + 1 + 1

5 = 1 + 1 + 2 + 1

5 = 1 + 1 + 1 + 2

5 = 2 + 2 + 1

5 = 2 + 1 + 2

5 = 1 + 2 + 2

输入

输入文件中包含多个测试数据。每个测试数据占一行,为一个正整数n1≤n≤40。输入文件最后一行为0,代表输入结束。

输出

对输入文件中的每个测试数据,输出n步台阶有多少种走法。

样例输入

2
5
0

样例输出

2
8


#include<bits/stdc++.h>
using namespace std;
int f(int n){
    if(n==1){
        return 1;
    }
    if(n==2){
        return 2;
    }else{
        return f(n-1)+f(n-2);
    }
}
int main(){
    int n;
    while(~scanf("%d",&n)&&n){
        cout<<f(n)<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/m0_37345402/article/details/80747280
今日推荐