#2526. 2024 CSP-J1 真题 · 阅读程序2

2024 CSP-J1 真题 · 阅读程序2

2024 CSP-J1 真题(120 分钟,总分 100 分)

阅读程序2(共 6 题,共 15.0 分)

1.

#include <iostream>
#include <vector>
using namespace std;

int compute(vector<int>& cost) {
    int n = cost.size();
    vector<int> dp(n+1, 0);
    dp[1] = cost[0];

    for (int i = 2; i <= n; i++) {
        dp[i] = min(dp[i-1], dp[i-2]) + cost[i-1];
    }
    return min(dp[n], dp[n-1]);
}

int main() {
    int n;
    cin >> n;
    vector<int> cost(n);
    for (int i = 0; i < n; i++) {
        cin >> cost[i];
    }
    cout << compute(cost) << endl;
    return 0;
}

当输入的cost数组为{10,15,20}时,程序的输出为15。

{{ select(1) }}

  • 正确
  • 错误

2. 如果将dp[i-1]改为dp[i-3],程序可能会产生编译错误。

{{ select(2) }}

  • 正确
  • 错误

3. 程序总是输出cost数组中最小的元素。

{{ select(3) }}

  • 正确
  • 错误

4. 当输入的cost数组为{1,100,1,1,1,100,1,1,100,1}时,程序的输出为()。

{{ select(4) }}

  • "6"
  • "7"
  • "8"
  • "9"

5. 如果输入的cost数组为{10,15,30,5,5,10,20},程序的输出为()。

{{ select(5) }}

  • "25"
  • "30"
  • "35"
  • "40"

6. 若将代码中的min(dp[i-1],dp[i-2])+cost[i-1]修改为dp[i-1]+cost[i-2],输入cost数组为{5,10,15}时,程序的输出为()。

{{ select(6) }}

  • "10"
  • "15"
  • "20"
  • "25"