-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNot_01_Package.cpp
120 lines (113 loc) · 2.48 KB
/
Not_01_Package.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// Created by 86184 on 2021-06-23.
//
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
struct TGods
{
int id;//物品编号
float w,v;//重量 价值
float x;//比例
TGods():w(0),v(0),x(0){}//初始化
};
bool bgreat(const TGods &x,TGods &y)
{
return x.v/x.w > y.v/y.w;
}
template <typename T>//将背包问题进行类的封装
class solution{
public:
int num;
T limit;//限制最大重量
TGods *array;
queue<int>ways;
T value;//总价值
T weight;//总重量
T cc;//剩余容量
solution(){
cout<<"请输入货物数目和限制重量"<<endl;
cin>>num>>limit;
array=new TGods[num];
for (int i = 0; i < num; ++i) {
array[i].id=i+1;
}
cout<<"请依次输入各物品重量"<<endl;
for (int i = 0; i < num; ++i) {
cin>>array[i].w;
}
cout<<"请依次输入各物品价值"<<endl;
for (int i = 0; i < num; ++i) {
cin>>array[i].v;
}
cc=limit;
}
void greedy_solution();
void show();
~solution(){
delete[] array;//释放内存
}
};
template<typename T>
void solution<T>::greedy_solution() {
sort(array,array+num,bgreat);
for (int i = 0; i < num; ++i) {
if (cc==0)
break;
if (cc>array[i].w)
{
array[i].x=1;
cc-=array[i].w;
value+=array[i].v;
weight+=array[i].w;
continue;
}
float x=cc/array[i].w;//此时已经不够一次完整的取出物品了
array[i].x=x;
value+=array[i].v*x;
weight+=array[i].w*x;
break;
}
}
template<typename T>
void solution<T>::show() {
cout<<"max weight:"<<weight<<endl;
cout<<"max value:"<<value<<endl;
cout<<"The choice of package are :";
for (int i = 0; i < num; ++i) {
if (array[i].x<=0)
{
break;
}
cout<<"package"<<array[i].id<<"("<<array[i].x<<")"<<"--";
}
cout<<endl;
cout<<"the form of packages is blow:"<<endl;
for (int j = 0; j < num; ++j) {
cout<<array[j].id<<" ";
}
cout<<endl;
for (int i = 0; i < num; ++i) {
cout<<array[i].w<<" ";
}
cout<<endl;
for (int i = 0; i <num; ++i) {
cout<<array[i].v<<" ";
}
cout<<endl;
for (int i = 0; i < num; ++i) {
cout<<array[i].x<<" ";
}
}
int main()
{
solution<float> test;
test.greedy_solution();
test.show();
test.~solution();
}
//测试数据
//5 26
//5 6 9 3 5
//7 4 10 3 4