1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #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; } |
0 of 6 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 6 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
1、当输入的 cost
数组为 {10,15,20} 时,程序的输出为15。( )
dp[i-1]
改为 dp[i-3]
,程序可能会产生编译错误。( )3、程序总是输出 cost
数组中最小的元素。( )
4、当输入的 cost
数组为 {1,100,1,1,1,100,1,1,100,1} 时,程序的输出为( )。
5、如果输入的 cost
数组为 {10,15,30,5,5,10,20},程序的输出为( )。
6、若将代码中的 min(dp[i-1], dp[i-2]) + cost[i-1]
修改为 dp[i-1] + cost[i-2]
,输入 cost
数组为 {5,10,15}时,程序的输出为( )