题解 | #N阶楼梯上楼问题#
N阶楼梯上楼问题
https://www.nowcoder.com/practice/c978e3375b404d598f1808e4f89ac551
#include <bits/stdc++.h>
using namespace std;
int f(int n) {
if(n== 1|| n== 2) {
return n;
}
return f(n-1)+ f(n-2);
}
int f2(int n) {
int dp[100];
memset(dp, 0, sizeof(dp));
dp[0]= 0;
dp[1]= 1;
dp[2]= 2;
for(int i=3; i<=n; i++) {
dp[i]= dp[i-1]+ dp[i-2];
}
return dp[n];
}
int main() {
int n;
while(cin>> n) {
cout<< f2(n)<< endl;
}
}
// 64 位输出请用 printf("%lld")
基恩士成长空间 434人发布