算法设计方案与分析实验报告—01背包问题 联系客服

发布时间 : 星期二 文章算法设计方案与分析实验报告—01背包问题更新完毕开始阅读3315c1a7ce22bcd126fff705cc17552706225e76

}

x[n] = (m[n][c]) ? 1:0;

for(int y = 1; y <= n; y++)

cout << x[y] << \

cout << endl;

return x[n];

}

void main() {

int

n, c;

int **m;

cout << \欢迎使用&&&&&&&&&&&&&&&&&&&\

cout << \请输入物品个数: \ cin >> n ;

cout << endl << \请输入背包的承重:\

cin >> c;

int *v = new int[n+1];

cout << endl << \请输入每个物品的价值 (v[i]): \ for(int i = 1; i <= n; i++)

cin >> v[i];

int *w = new int[n+1];

cout << endl << \请输入每个物品的重量 (w[i]): \ for(int j = 1; j <= n; j++)

cin >> w[j];

int *x = new int[n+1]; m = new int* [n+1];

//动态的分配二维数组

for(int p = 0; p < n+1; p++)

m[p] = new int[c+1];

knapsack (v, w, m, c, n);

traceback(x, w, m, c, n);

}

0-1

背包问题程序

【运行结果】