第30887题 单选题
以下C++动态规划实现爬楼梯问题的代码中,横线处应填入的正确语句是?
#include <iostream>
#include <vector>
using namespace std;

int climbStairs(int n) {
    if (n <= 1) return 1;
    vector<int> dp(n + 1);
    dp[1] = 1;
    dp[2] = 2;
    for (int i = 3; i <= n; i++) {
        ____________ // 横线处为待填入代码
    }
    return dp[n];
}

int main() {
    cout << climbStairs(5) << endl; // 预期输出为8
    return 0;
}

爬楼梯问题规则:每次仅可以走1阶或者2阶楼梯,求走到第n阶的总方法数。

{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
程序运行统计
暂无判题统计
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析