01背包是典型的动态规划问题,说白了就是按照体积来装物体,如果V[i][j]体积j大于物品体积v,
V[i][j]=max{V{i-1][j],V[i-1][j-v]+s}
#include<iostream>
using namespace std;
#include<vector>
int V[200][200] = { 0 };
int main()
{int n,nw;//体积,物品cin >> nw>>n;vector<int> value;vector<int> weight;int k = 0;for (k; k < nw; k++){int vv, ww;cin >> ww >> vv;value.push_back(vv);weight.push_back(ww);}int maxv = 0;int i = 0;for (i = 1; i <= nw; i++){int j = 1;for (j; j <= n; j++)//体积{V[i][j] = V[i-1][j];if (j >= weight[i-1]){V[i][j] = max(V[i][j], V[i - 1][j - weight[i-1]] + value[i-1]);}maxv = max(maxv, V[i][j]);}}cout << maxv<<endl;
}