求一个背包问题程序---数据结构课设的(要长一些)可运行

来源:百度知道 编辑:UC知道 时间:2024/06/15 10:52:35

以前做课设用的程序,完全符合你的要求,有什么不懂再问我

// 0-1 Knapsack.cpp
//

#include <iostream>
#include <iomanip>
using namespace std;

int c; //capacity of the knapsack
int n; //the number of items
int *w; //weight of items
int *p; //value of items
int cw; //current weight
int cp; //current value
int *choose; //current loading status
int bestp; //optimal value
int *bestc; //optimal loading solution

//recursion
void backtrack(int t)
{
int i,j;
if(t>n)
{
for(i=1;i<=n;i++)
{
cout<<setw(6)<<choose[i];
}
cout<<setw(8)<<cw;
cout<<setw(8)<<cp<<endl;
if(cp>bestp)
{
bestp=cp;
for(j=1;j<=n;j++)
{
bestc[j]=choose[j];
}
}
}
else
{
for(i=0;i<=1;i++)
{
if(i==0)
{
choo