Knapsack problem/Continuous: Difference between revisions
Content deleted Content added
Added Forth (4tH) version |
Some optimization in Forth example |
||
Line 456:
( a -- n)
: price/weight dup 2 cells + @c swap cell+ @c / ;
: weight@ @ cell+ @c ; ( a -- n)
: .item @ @c count type cr ; ( a --)
\ how to sort: on price/weight
:noname >r price/weight r> price/weight > ; is precedes
: knapsack ( --)
(items)
begin
while
." Take all of the " dup
left over weight@ - to left cell+ \ adjust knapsack, increment item
repeat left 100 * dup \ so how much is left?
\ if room, take as much as possible
if ." Take " . ." grams of the "
;
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
|