标签:max data- ++ iostream 选择 esc NPU std tle
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - obj_weight] + obj_price);
dp[i][j] = dp[i - 1][j];
#include <iostream> using namespace std; #define NUM 1000 int dp[NUM][NUM]; int max(int a, int b) { return a > b ? a : b; } int main() { int n;//n个物品 int c;//背包容量 cin >> n >> c; int weight_price_table[100][2]; for (int i = 1; i <= n; i++) { cin >> weight_price_table[i][0] >> weight_price_table[i][1]; } //初始化边界 当i=0,j=0时 dp = 0 for (int i = 0; i < NUM; i++) { dp[0][i] = 0; dp[i][0] = 0; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= c; j++) { int obj_weight = weight_price_table[i][0]; int obj_price = weight_price_table[i][1]; if (j >= obj_weight) {//如果装得下 dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - obj_weight] + obj_price); } else {//装不下 dp[i][j] = dp[i - 1][j]; } } } cout << dp[n][c]; }
标签:max data- ++ iostream 选择 esc NPU std tle
原文地址:https://www.cnblogs.com/likeghee/p/11770388.html