Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Knapsack-problem-0-1/Eiffel/knapsack-problem-0-1-1.e
Normal file
40
Task/Knapsack-problem-0-1/Eiffel/knapsack-problem-0-1-1.e
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
local
|
||||
knapsack: KNAPSACKZEROONE
|
||||
do
|
||||
create knapsack.make (400)
|
||||
knapsack.add_item (create {ITEM}.make ("", 0, 0))
|
||||
knapsack.add_item (create {ITEM}.make ("map", 9, 150))
|
||||
knapsack.add_item (create {ITEM}.make ("compass", 13, 35))
|
||||
knapsack.add_item (create {ITEM}.make ("water", 153, 200))
|
||||
knapsack.add_item (create {ITEM}.make ("sandwich", 50, 160))
|
||||
knapsack.add_item (create {ITEM}.make ("glucose", 15, 60))
|
||||
knapsack.add_item (create {ITEM}.make ("tin", 68, 45))
|
||||
knapsack.add_item (create {ITEM}.make ("banana", 27, 60))
|
||||
knapsack.add_item (create {ITEM}.make ("apple", 39, 40))
|
||||
knapsack.add_item (create {ITEM}.make ("cheese", 23, 30))
|
||||
knapsack.add_item (create {ITEM}.make ("beer", 52, 10))
|
||||
knapsack.add_item (create {ITEM}.make ("suntan cream", 11, 70))
|
||||
knapsack.add_item (create {ITEM}.make ("camera", 32, 30))
|
||||
knapsack.add_item (create {ITEM}.make ("T-shirt", 24, 15))
|
||||
knapsack.add_item (create {ITEM}.make ("trousers", 48, 10))
|
||||
knapsack.add_item (create {ITEM}.make ("umbrella, ella ella", 73, 40))
|
||||
knapsack.add_item (create {ITEM}.make ("waterproof trousers", 42, 70))
|
||||
knapsack.add_item (create {ITEM}.make ("waterproof overclothes", 43, 75))
|
||||
knapsack.add_item (create {ITEM}.make ("note-case", 22, 80))
|
||||
knapsack.add_item (create {ITEM}.make ("sunglasses", 7, 20))
|
||||
knapsack.add_item (create {ITEM}.make ("towel", 18, 12))
|
||||
knapsack.add_item (create {ITEM}.make ("socks", 4, 50))
|
||||
knapsack.add_item (create {ITEM}.make ("book", 30, 10))
|
||||
knapsack.compute_solution
|
||||
end
|
||||
|
||||
end
|
||||
35
Task/Knapsack-problem-0-1/Eiffel/knapsack-problem-0-1-2.e
Normal file
35
Task/Knapsack-problem-0-1/Eiffel/knapsack-problem-0-1-2.e
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
class
|
||||
ITEM
|
||||
|
||||
create
|
||||
make, make_from_other
|
||||
|
||||
feature
|
||||
|
||||
name: STRING
|
||||
|
||||
weight: INTEGER
|
||||
|
||||
value: INTEGER
|
||||
|
||||
make_from_other (other: ITEM)
|
||||
-- Item with name, weight and value set to 'other's name, weight and value.
|
||||
do
|
||||
name := other.name
|
||||
weight := other.weight
|
||||
value := other.value
|
||||
end
|
||||
|
||||
make (a_name: String; a_weight, a_value: INTEGER)
|
||||
-- Item with name, weight and value set to 'a_name', 'a_weight' and 'a_value'.
|
||||
require
|
||||
a_name /= Void
|
||||
a_weight >= 0
|
||||
a_value >= 0
|
||||
do
|
||||
name := a_name
|
||||
weight := a_weight
|
||||
value := a_value
|
||||
end
|
||||
|
||||
end
|
||||
106
Task/Knapsack-problem-0-1/Eiffel/knapsack-problem-0-1-3.e
Normal file
106
Task/Knapsack-problem-0-1/Eiffel/knapsack-problem-0-1-3.e
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
class
|
||||
KNAPSACKZEROONE
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
|
||||
items: ARRAY [ITEM]
|
||||
|
||||
max_weight: INTEGER
|
||||
|
||||
feature
|
||||
|
||||
make (a_max_weight: INTEGER)
|
||||
-- Make an empty knapsack.
|
||||
require
|
||||
a_max_weight >= 0
|
||||
do
|
||||
create items.make_empty
|
||||
max_weight := a_max_weight
|
||||
end
|
||||
|
||||
add_item (item: ITEM)
|
||||
-- Add 'item' to knapsack.
|
||||
local
|
||||
temp: ITEM
|
||||
do
|
||||
create temp.make_from_other (item)
|
||||
items.force (item, items.count + 1)
|
||||
end
|
||||
|
||||
compute_solution
|
||||
local
|
||||
M: ARRAY [INTEGER]
|
||||
n: INTEGER
|
||||
i, j: INTEGER
|
||||
w_i, v_i: INTEGER
|
||||
item_i: ITEM
|
||||
final_items: LINKED_LIST [ITEM]
|
||||
do
|
||||
n := items.count
|
||||
create M.make_filled (0, 1, n * max_weight)
|
||||
from
|
||||
i := 2
|
||||
until
|
||||
(i > n)
|
||||
loop
|
||||
from
|
||||
j := 1
|
||||
until
|
||||
j > max_weight
|
||||
loop
|
||||
item_i := items [i]
|
||||
w_i := item_i.weight
|
||||
if w_i <= j then
|
||||
v_i := item_i.value
|
||||
M [(i - 1) * max_weight + j] := max (M [(i - 2) * max_weight + j], M [(i - 2) * max_weight + j - w_i + 1] + v_i)
|
||||
else
|
||||
M [(i - 1) * max_weight + j] := M [(i - 2) * max_weight + j]
|
||||
end
|
||||
j := j + 1
|
||||
end
|
||||
i := i + 1
|
||||
end
|
||||
io.put_string ("The final value of the knapsack will be: ")
|
||||
io.put_integer (M [(n - 1) * max_weight + max_weight]);
|
||||
io.new_line
|
||||
--compute the items that fit into the knapsack
|
||||
create final_items.make
|
||||
io.put_string ("We'll take the following items: %N");
|
||||
from
|
||||
i := n
|
||||
j := max_weight
|
||||
until
|
||||
i <= 1 or j <= 1
|
||||
loop
|
||||
item_i := items [i]
|
||||
w_i := item_i.weight
|
||||
if w_i <= j then
|
||||
v_i := item_i.value
|
||||
if M [(i - 1) * max_weight + j] = M [(i - 2) * max_weight + j] then
|
||||
else
|
||||
final_items.extend (item_i)
|
||||
io.put_string (item_i.name)
|
||||
io.new_line
|
||||
j := j - w_i
|
||||
end
|
||||
else
|
||||
end
|
||||
i := i - 1
|
||||
end
|
||||
end
|
||||
|
||||
feature {NONE}
|
||||
|
||||
max (a, b: INTEGER): INTEGER
|
||||
-- Max of 'a' and 'b'.
|
||||
do
|
||||
Result := a
|
||||
if a < b then
|
||||
Result := b
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue