学院实验报告要求实现一个背包问题(需要用回溯法,用堆栈一块的知识求所有能装满背包的解,不知道到为什么网上能找到的背包问题是求总价值最大qaq),然后我就写了下面的程序(需要我们一下子完成四个问题,我还只写了背包问题的部分qaq),Vs的检查没有问题但是就是没法运行,调试我也看不懂qaq,就来提问了
//实验目的:实现背包问题、地图四染色、列车重排算法、投资组合问题
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#define MaxStackValue 50
#define Products ElementType
using namespace std;
//背包问题(回溯法)
//存储货物数据的类
class Products {
public:
string number;
string name;
string place;
int weight;
void Print_Value(){
cout << setw(10) << left << number << setw(10) << left << name << setw(10) << right << place << setw(10) << right << weight << endl;
};
Products(string n, string m, string p, int w): number(n), name(m), place(p), weight(w){}
Products(){number = " "; name = " "; place = " "; weight = 0;}
};
//存储栈的结构体
struct PdStack{
int index;
Products knapsack[20];
PdStack();
};
//创建包含Products类内数据的一维数组
void Element_IN(Products x, int n, Products element[]){
for(int i = n-1; i >= 1; i--){
element[i + 1] = element [i];
}
element[0] = x;
}
//向类Products内添加新数据
void New_Product(ElementType elements[], int n){
string number_new, name_new, place_new;
int weight_new;
cout << "请输入货物编号:" << endl;
getline(cin, number_new);
cout << "请输入货物名称:" << endl;
getline(cin, name_new);
cout << "请输入存放位置:" << endl;
getline(cin, place_new);
cout << "请输入货物重量:" << endl;
cin >> weight_new;
cout << "您输入的货物信息为:" << endl;
cout << setw(10) << left << "编号" << setw(10) << left << "名称" << setw(10) << right << "位置" << setw(10) << right << "重量" << endl;
cout << setw(10) << left << number_new << setw(10) << left << name_new << setw(10) << right << place_new << setw(10) << right << weight_new << endl;
Products new_product(number_new, name_new, place_new, weight_new);
Element_IN(new_product, n, elements);
cout << "需要继续添加货物信息吗?(Y/N)" << endl;
string input_get;
getline(cin, input_get);
if("Y" == input_get){
New_Product(elements, n);
}
}
//回溯法内核
void TraverseStack(PdStack &S, int n, ElementType elements[],int ini){
for(int i = ini; i < n; i++){
if(i == n-1){break;}
else if(elements[i].weight + S.index < MaxStackValue){
S.index += elements[i].weight;
for(int j = n-1; j >= 1; j--){
S.knapsack[j + 1] = S.knapsack[j];
}
S.knapsack[0] = elements[i];
}
else if(elements[i].weight + S.index > MaxStackValue){
S.index -= S.knapsack[0].weight;
for(int j = 0; j < n-1; j++){
S.knapsack[j] = S.knapsack[j+1];
ini ++;
TraverseStack(S, n, elements, ini);
break;
}
}
else if(elements[i].weight + S.index == MaxStackValue){
for(int j = n-1; j >= 1; j--){
S.knapsack[j + 1] = S.knapsack[j];
}
S.knapsack[0] = elements[i];
}
}
}
//建立示例
void Example(){
Products *elements_eg = new Products[6]{
Products("10000", "AAAAA", "wwww0", 20),
Products("10001", "BBBBB", "wwww1", 30),
Products("10002", "CCCCC", "wwww2", 40),
Products("10003", "DDDDD", "wwww3", 50),
Products("10004", "EEEEE", "wwww4", 80),
Products("10005", "FFFFF", "wwww5", 10)
};
PdStack S_eg;
S_eg.index = 0;
int ini_eg = 0;
TraverseStack(S_eg, 6, elements_eg, ini_eg);
cout << setw(10) << left << "编号" << setw(10) << left << "名称" << setw(10) << right << "位置" << setw(10) << right << "重量" << endl;
for(int i = 0; i < 6; i++){
S_eg.knapsack[i].Print_Value();
}
ini_eg = 0;
delete[] elements_eg;
}
//背包问题主函数
void Knapsack(){
cout << "现在展示背包问题。" << '\n' << "需要演示范例吗?(Y/n)" << endl;
string input1;
getline(cin, input1);
if("Y" == input1){
Example();
}
cout << "需要手动输入货物数据吗?(Y/N)" << endl;
string input2;
getline(cin, input2);
if("Y" == input2){
int Total_Volume, n;
int ini = 0;
ElementType *elements = new ElementType[n];
PdStack S;
S.index = 0;
cout << "请输入背包的总体积:" << endl;
cin >> Total_Volume;
cout << "请输入货物总数:" << endl;
cin >> n;//本来应该另外设计函数统计的,但是太麻烦了
cout << "现在新建一个货物数据:" << endl;
New_Product(elements, n);
cout << "现在开始计算背包问题的所有解:";
TraverseStack(S, n, elements, ini);
cout << setw(10) << left << "编号" << setw(10) << left << "名称" << setw(10) << right << "位置" << setw(10) << right << "重量" << endl;
delete[]elements;
}
}
int main() {
cout << "你想要求解的问题是:(输入序号)" << '\n' << "A、背包问题 B、地图四染色" << '\n' <<"C、列车重排算法 D、投资组合问题" << endl;
string input;
getline(cin, input);
if("A" == input){
Knapsack();
}
else{
cout << "无效输入!" << endl;
}
}
报错是这样的:
你先找到求价值最大的代码,稍微把里面的if改改,找到解也不要break而是都print出来,不就行了
对于提供的参考资料,回答问题的帮助并不大。建议提供更具体和明确的问题和描述。