-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackpack.cpp
More file actions
78 lines (76 loc) · 1.78 KB
/
Copy pathBackpack.cpp
File metadata and controls
78 lines (76 loc) · 1.78 KB
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
#include <iostream>
int max(int number1, int number2) {
if (number1 > number2) {
return number1;
} else return number2;
}
int main()
{
int count;
int max_weight;
std::cout << "Input max weight ";
std::cin >> max_weight;
std::cout << "Input max count ";
std::cin >> count;
int** list=new int*[count+2];
int* cost_list= new int[count+1];
int* weight_list= new int[count+1];
for (int i = 0; i < count+2; i++) list[i] = new int[max_weight+2];
for (int i=0;i<count+2;i++)
{
for (int j=0;j<max_weight+2;j++)
{
list[i][j]=0;
}
}
for (int i=2;i<max_weight+2;i++)
{
list[0][i]=i-1;
}
for (int i=2;i<count+2;i++)
{
int x;
std::cout << "Input " << i-1 << " weight ";
std::cin >> x;
weight_list[i]=x;
list[i][0]=i-1;
std::cout << "Input " << i-1 << " cost ";
std::cin >> x;
cost_list[i]=x;
}
for (int i=2;i<count+2;i++)
{
for (int j=2;j<max_weight+2;j++)
{
if (j-1<weight_list[i])
{
list[i][j]=list[i-1][j];
} else if(j-1>=weight_list[i])
{
list[i][j]=max(list[i-1][j],list[i-1][j-weight_list[i]]+cost_list[i]);
}
}
}
for (int i=0;i<count+2;i++)
{
for (int j=0;j<max_weight+2;j++)
{
std::cout <<list[i][j] << '\t';
}
std::cout << std::endl;
}
int i{count+1};
int j{max_weight+1};
std::cout << "Комбинация ";
while (i-1>0 and j>0)
{
if(list[i-1][j]==list[i][j])
{
i--;
continue;
}
std::cout << i-1 << " ";
j-=weight_list[i];
i--;
}
}