1205.n阶楼梯上楼问题

题目描述:

N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式。(要求采用非递归)

输入:

输入包括一个整数N,(1<=N<90)。

输出:

可能有多组测试数据,对于每组数据,
输出当楼梯阶数是N时的上楼方式个数。

样例输入:
4
样例输出:
5


#include<iostream>
using namespace std;

int fac(int x){
    if(x==1) return 1;
    else if(x==2) return 2;
    else return fac(x-1)+fac(x-2);
}

int main(){
    int n;
    while(cin>>n){
        cout<<fac(n)<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/bernieloveslife/p/9735251.html