编辑代码

#include <iostream>
#include<string>
using namespace std;
int weight[5]={0,1,4,3,1};
int value[5]={0,1500,3000,2000,2000};
int dp[5][5]={0};
string object[5]={" ","Guitar","Sound Equipment","Notebook","IPhone"};

int max(int x,int y)
{
    if(x<y)
    x=y;
    return x;
}
void D()
{
 for(int i=1;i<5;i++)
    {
    for(int j=1;j<5;j++)
   { 
   if(weight[i]>j)
    dp[i][j]=dp[i-1][j];
    else
    dp[i][j]=max(dp[i-1][j],dp[i-1][j-weight[i]]+value[i]);
   }
}
     cout<<"一共价格为:"<<dp[4][4]<<endl;
 } 
 void find(int h,int g)
 {
   if(dp[h][g]==dp[h-1][g-weight[h]]+value[h]) 

{

    cout<<object[h]<<"  ";
    find(h-1,g-weight[h]);
}
}
 
int main() 
{

  D();
   cout<<"背包装进物品为:" ; 
 find(4,4);
 
return 0;
}