题意:类比0-1背包问题。在背包容量为C的情况下,从N件物品中选择x件物品放入背包,使得物品总容量不超过背包容量且物品总价值达到最大。
代码:
#include <iostream> using namespace std; int dp[101][1001]; int main() { int n,c; while(cin >> c >> n){ for(int i = 0; i < 101; i ++){ for(int j = 0; j < 1001; j ++) dp[i][j] = 0; } int *w = (int*)malloc(sizeof(int)*(n+1)); int *v = (int*)malloc(sizeof(int)*(n+1)); for(int i = 1; i <= n; i ++) cin >> w[i] >> v[i]; for(int i = 1; i <= n; i ++){ for(int j = 1; j <= c; j ++){ if(j < w[i]) dp[i][j] = dp[i-1][j]; else dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i]] + v[i]); } } cout << dp[n][c] << endl; } return 0; }
标签:malloc,背包,KY66,问题,int,点菜,sizeof,101 From: https://www.cnblogs.com/jianqiao123/p/17019411.html