0-1背包问题经典算法(一维数组实现)

//在前两个算法的基础上,算法的空间复杂度由o(GOODSNUM*CAPACITY)降低到o(CAPACITY)

#include "iostream"
#define CAPACITY 10
#define GOODSNUM 6

using namespace std;

void main()
{
 int nVolume[GOODSNUM] = {1,2,3,5,2,1};
 int nValue[GOODSNUM] = {10,12,40,40,40,15};
 int nKnapsack[CAPACITY+1] = {0};
 int itemIndex,capIndex;
 for (itemIndex=0;itemIndex {
  for (capIndex=CAPACITY;capIndex>0;capIndex--)
  {
   if (capIndex>=nVolume[itemIndex] && nKnapsack[capIndex]   {
    nKnapsack[capIndex] = nKnapsack[capIndex-nVolume[itemIndex]]+nValue[itemIndex];
   }
   else
    nKnapsack[capIndex] = nKnapsack[capIndex];
  }
 }
 cout<<"The max value is: "<}

你可能感兴趣的:(algorithm)