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

 

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

using namespace std;

void main()
{
 int i=1,j;
 int v[GOODSNUM] = {10,12,40,40,40,15};
 int c[GOODSNUM] = {1,2,3,5,2,1};
 int fun[GOODSNUM+1][CAPACITY+1];
 for (i=1;i<=GOODSNUM;i++)
 {
  fun[i][0] = 0;
 }
 for (i=1;i<=CAPACITY;i++)
 {
  fun[0][i] = 0;
 }
 for (i=1;i<=GOODSNUM;i++)
 {
  for (j=1;j<=CAPACITY;j++)
  {
   if (j   {
    fun[i][j] = fun[i-1][j];
   }
   else if (fun[i-1][j]    {
     fun[i][j] = fun[i-1][j-c[i-1]] + v[i-1];
    }
   else
    fun[i][j] = fun[i-1][j];
  }
 }
 cout<<"The max value is: "<}

你可能感兴趣的:(algorithm)