C++版本:
01揹包
for (int i = 0; i < n; i++) {
for (int j = M; j >= w[i]; j--) {
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
}
完全揹包
for (int i = 0; i < n; i++) {
for (int j = w[i]; j <= M; j++) {
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
}
關注我檢視更多精華內容