-
Notifications
You must be signed in to change notification settings - Fork 0
/
Beibao.cpp
74 lines (59 loc) · 1.28 KB
/
Beibao.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
#include "stdafx.h"
#include "Beibao.h"
Beibao::Beibao()
{
ioIn();
}
Beibao::~Beibao()
{
}
int Beibao::getMaxValue(){
return getMaxValue(maxWight, num, weight, value);
}
int Beibao::getMaxValue(int maxWeight, int num, int *weight, int *value){
int temp = 0;
nvalue = new int*[num+1];
for (int i = 0; i <= num; i++){
nvalue[i] = new int[maxWight + 1];
nvalue[i][0] = 0;
}
for (int i = 0; i <= maxWeight; i++){
nvalue[0][i] = 0;
}
for (int i = 1; i <= num; i++){
for (int j = 1; j <= maxWeight; j++){
if (weight[i-1] > j){
nvalue[i][j] = nvalue[i - 1][j];
}
else{
nvalue[i][j] = max(nvalue[i - 1][j], nvalue[i - 1][j - weight[i-1]] + value[i-1]);
}
}
}
int max = maxWeight;
for (int i = num; i>0; i--){
if (max <= 0){
break;
}
if (nvalue[i][max] > nvalue[i - 1][max]){
select[i-1] = 1;
max -= weight[i - 1];
}
}
for (int i = 0; i < num; i++){
cout << select[i];
}
cout << endl;
cout << nvalue[num][maxWeight] << endl;
return nvalue[num][maxWeight];
}
void Beibao::ioIn(){
cin >> maxWight >> num;
weight = new int[num];
value = new int[num];
select = new int[num];
for (int i = 0; i < num; i++){
cin >> weight[i] >> value[i];
select[i] = 0;
}
}