第20849题 判断题
将0-1背包一维动态规划的内层逆序循环改为正序遍历,是否仍能得到正确答案?
int main() {
  int W = 5;
  int w[] = {2, 3, 4};
  int v[] = {10, 1, 1};
  int n = 3;
  int dp[6] = {0};

  for (int i = 0; i < n; i++) {
    for (int j = W; j >= w[i]; j--) { // ← 逆序!
      dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
    }
  }
  cout << dp[W];
}
{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
程序运行统计
暂无判题统计
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析