爬楼梯游戏 题解:为了防止爆内存,我们事先准备好数组
#include <bits/stdc++.h>
using namespace std;
const int p=1e9+7;
int main() {
std::vector<int> a(1000001);
a[1]=1,a[2]=2;
for(int i=3;i<=1000001;i++){
a[i]=(a[i-1]+a[i-2])%p;
}
int n;
while(cin>>n){
cout<<a[n]<<endl;
}
}
这是上楼梯问题的改版,他的dp函数同斐波那契数列的数学公式
登录后发布评论
暂无评论,来抢沙发