using namespace std;
struct Commodity {
string commodityname;
int weight;
int price;
Commodity* next;
Commodity(string n, int w, int p) {
weight = w;
price = p;
commodityname = n;
next = NULL;
}
Commodity() {
weight = 0;
price = 0;
commodityname = "";
next = NULL;
}
Commodity(Commodity* commodity) {
weight = commodity->weight;
price = commodity->price;
commodityname = commodity->commodityname;
next = commodity->next;
}
};
struct SELECT {
int sum;
Commodity* head;
SELECT() {
sum = 0;
head = new Commodity();
}
SELECT(SELECT* select) {
sum = select->sum;
head = new Commodity();
head->next = select->head->next;
}
};
SELECT* fun( Commodity* commodity, int CommodityCount,int capacity) {
int minweight = commodity[0].weight;
for (int i = 1; i < CommodityCount; i++)
{
int curweight = commodity[i].weight;
if (curweight < minweight) {
minweight = curweight;
}
}
if (capacity < minweight) {
return NULL;
}
SELECT** Array = new SELECT * [CommodityCount]();
int weightcount = capacity + 1;
for (int i = 0; i < CommodityCount; i++) {
Array[i] = new SELECT[weightcount]();
}
for (int i = 0; i < CommodityCount; i++)
{
int curWeight = commodity[i].weight;
int curValue = commodity[i].price;
for (int weight = minweight; weight < weightcount; weight++)
{
int prevalue = 0;
int curvalue = 0;
if (i > 0) {
prevalue = Array[i - 1][weight].sum;
}
if (weight >= curWeight) {
curvalue = curValue;
}
if (i > 0 && weight > curWeight) {
curvalue=curvalue+ Array[i - 1][weight - curWeight].sum;
Array[i][weight].head->next = Array[i - 1][weight - curWeight].head->next;
}
int maxvalue = prevalue;
if (maxvalue < curvalue) {
maxvalue = curvalue;
commodity[i].next = Array[i][weight].head->next;
Array[i][weight].head->next = new Commodity(&commodity[i]);
}
else {
Array[i][weight].head->next = Array[i - 1][weight].head->next;
}
Array[i][weight].sum = maxvalue;
}
}
SELECT* select = new SELECT(&Array[CommodityCount - 1][weightcount - 1]);
return select;
}
void PrintF(SELECT*select)
{
if (select->sum > 0)
{
Commodity* commodity = select->head->next;
Commodity* prev = NULL;
cout << "选择盗窃物品为";
while (commodity != NULL) {
cout << commodity->commodityname << ", ";
prev = commodity;
commodity = commodity->next;
delete prev;
}
cout << "最大价值为 " << select->sum << endl;
cout << endl;
}
}
int main() {
cout << "例1:" << endl;
int capacity = 4;
Commodity commoditys1[] = {
Commodity("Guitar", 1, 1500),
Commodity("Sound Equipment", 4, 3000),
Commodity("Notebook", 3, 2000),
Commodity("HUAWEI", 1, 6000)
};
int commodityCount = sizeof(commoditys1) / sizeof(Commodity);
SELECT* select1 = NULL;
select1 = fun(commoditys1, commodityCount,capacity);
PrintF(select1);
cout << "例2:" << endl;
capacity = 3;
Commodity commoditys2[] = {
Commodity("Guitar", 1, 1500),
Commodity("Sound Equipment", 3, 3000),
Commodity("Notebook", 3, 2000),
Commodity("HUAWEI", 2, 6000)
};
commodityCount = sizeof(commoditys2) / sizeof(Commodity);
SELECT* select2 = NULL;
select2 = fun(commoditys2, commodityCount,capacity);
PrintF(select2);
cout << "例3:" << endl;
capacity = 5;
Commodity commoditys3[] = {
Commodity("Guitar", 1, 1500),
Commodity("Sound Equipment", 3, 3000),
Commodity("Notebook", 3, 2000),
Commodity("HUAWEI", 2, 6000)
};
commodityCount = sizeof(commoditys3) / sizeof(Commodity);
SELECT* select3 = NULL;
select3 = fun(commoditys3, commodityCount, capacity);
PrintF(select3);
}