-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_program_2.demo
executable file
·68 lines (59 loc) · 1.1 KB
/
test_program_2.demo
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
// Knapsack problem
procedure knapsack
{
// Input:
// Values (stored in array v)
// Weights (stored in array w)
// Number of distinct items (n)
// Knapsack capacity (W)
int i,j;
int n, W;
int v[1:100], w[1:100];
int m[1:100, 1:100];
write 'n';
read n;
write 'W';
read W;
write 'v';
for i = 1 to n by 1
{
read v[i];
}
write 'w';
for i = 1 to W by 1
{
read w[i];
}
for j = 0 to W by 1
{
m[0, j] = 0;
}
for i = 1 to n by 1
{
for j = 0 to W by 1
{
if (w[i-1] > j) then
{
m[i, j] = m[i-1, j];
}
else
{
if (m[i-1, j] > m[i-1, j-w[i-1]] + v[i-1]) then
{
m[i, j] = m[i-1, j];
}
else
{
m[i, j] = m[i-1, j-w[i-1]] + v[i-1];
}
}
}
}
for i = 1 to n by 1
{
for j = 0 to W by 1
{
write m[i,j];
}
}
}