L1-7 超级楼梯2

###Problem Description
有一超级楼梯,共无限级。刚开始时你在地面,你可以一步跨上第一级,也可以一步跨上第二级。
假设你每次只能向上跨一级或二级,那么你要走上第N级,共有多少种走法?
###Input
输入数据首先包含若干整数N(1<=N<=45),表示楼梯的级数。
###Output
对于每个测试实例N,请输出走到第N级楼梯的走法的数量。

###Sample Input

2
5
8

###Sample Output

2
8
34

###Sample Input

42
43
44
45

###Sample Output

433494437
701408733
1134903170
1836311903
#include <bits/stdc++.h>
using namespace std;

int count(int a){
    int pre=1,now=2;
    int kk=0;
    for(int i=3;i<=a;i++){
        kk=pre+now;
        pre=now;
        now=kk;
    }
    return kk;
}

int main(){
    int a;
    while(~scanf("%d",&a)){
        if(a==0) cout<<"0"<<endl;
        else if(a==1) cout<<"1"<<endl;
        else if(a==2) cout<<"2"<<endl;
        else {
            int k=count(a);
            cout<<k<<endl;
        }
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值