37 lines
966 B
Plaintext
37 lines
966 B
Plaintext
link printf
|
|
|
|
procedure main()
|
|
room := 15
|
|
every (x := !(choices := get_items())).uprice := x.price / x.weight
|
|
choices := reverse(sortf(choices,4))
|
|
|
|
every (value := 0, x := !choices) do {
|
|
if x.weight <= room then {
|
|
printf("Take all of the %s (%r kg) worth $%r\n",x.name,x.weight,x.price)
|
|
value +:= x.price
|
|
room -:= x.weight
|
|
}
|
|
else {
|
|
fvalue := x.uprice * room
|
|
printf("Take (%r kg) of the %s worth $%r\n",room,x.name,fvalue)
|
|
value +:= fvalue
|
|
break
|
|
}
|
|
}
|
|
printf("Total value of a full knapsack is $%r\n",value)
|
|
end
|
|
|
|
record item(name,weight,price,uprice)
|
|
|
|
procedure get_items()
|
|
return [ item("beef", 3.8, 36),
|
|
item("pork", 5.4, 43),
|
|
item("ham", 3.6, 90),
|
|
item("greaves", 2.4, 45),
|
|
item("flitch", 4.0, 30),
|
|
item("brawn", 2.5, 56),
|
|
item("welt", 3.7, 67),
|
|
item("salami", 3.0, 95),
|
|
item("sausage", 5.9, 98) ]
|
|
end
|