#include<iostream>#include<math.h>usingnamespacestd;
int inOut[5];
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 = 5;
int weight[5] = {6,5, 4,2,1};
int valueAll[5] = {5,3,5 ,3,2};
bagProblem(0, weight, valueAll);
cout << "最终的结果是:" << value << endl;
cout << endl;
return0;
}