0-1背包问题
参考
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <string>
using namespace std;
int getMostValue(vector<int>& value, vector<int>& weight,int packageWeight)
{
vector<vector<int>> help(value.size() + 1, vector<int>(packageWeight + 1,0));
for (int i = 1; i <= value.size(); ++i)
{
for (int j = 1; j <= packageWeight; ++j)
{
if (j >= weight[i-1])
help[i][j] = std::max(help[i - 1][j - weight[i-1]] + value[i-1], help[i - 1][j]);
else
help[i][j] = help[i - 1][j];
}
}
return help[value.size()][packageWeight];
}
int main(int argc, char *argv[])
{
vector<int> value{0, 60, 100, 120};
vector<int> weight{0, 10, 20, 30};
int packageWeight = 50;
int res = getMostValue(value, weight, packageWeight);
return res;
}