#include<iostream>#include<math.h>usingnamespacestd;
int inOut[4];
int value;
int bagVolume = 9;
boolbagConstraint(int m, int weight[]){
int allweight = 0;
for (int i = 0; i <= m; ++i)
{
allweight += inOut[i] * weight[i];
}
return allweight <= bagVolume;
}
voidbagProblem(int m, int weight[], int valueAll[]){
if (m == 4)
{
int sum = 0;
for (int i = 0; i < m; ++i)
{
sum += valueAll[i] * inOut[i];
}
if (sum > value)
{
value = sum;
}
}
else {
for (int i = 0; i < 2; ++i)
{
inOut[m] = i;
if (bagConstraint(m, weight))
{
bagProblem(m + 1, weight, valueAll);
}
}
}
}
intmain(int argc, charconst* argv[]){
int num = 4;
int weight[4] = { 2,3,4,5 };
int valueAll[4] = { 3,4,5,6 };
bagProblem(0, weight, valueAll);
cout << "最终的结果是:" << value << endl;
cout << endl;
return0;
}