Knapsack problem/Continuous: Difference between revisions
Content added Content deleted
m (→version 1: changed comment about sort method.) |
m (→version 1: removed generic width calculations and used the title word lengths.) |
||
Line 1,932:
if maxW=='' | maxW==',' then maxW=15 /*burglar's knapsack max weight. */
if ddig=='' | ddig==',' then ddig= 2 /*# of decimal digits in FORMAT. */
wL=length(' weight '); nL=length('total weight');
totW=0; totV=0
do j=1 while @.j\==''; parse var @.j n.j w.j v.j .
end /*j*/ /* [↑] assign to separate lists.*/
items=j-1 /*ITEMS: the # of items in @ list*/
call show 'unsorted item list' /*display a header and the @ list*/
/* [↓] sort is OK for short lists*/
do sorts=2 to items /*sort:
_n=n.sorts; _w=w.sorts; _v=v.sorts /*calc. placeholders for DO loop.*/
do k=sorts-1 by -1 to 1 while v.k/w.k < _v/_w /*order it*/
Line 1,955 ⟶ 1,948:
call hdr "burglar's knapsack contents"
call sep
call sy left('total weight',nL,'─'), format(totW,,ddig)
Line 1,975 ⟶ 1,968:
────────────────unsorted item list────────────────
flitch
beef
pork
greaves
brawn
welt
ham
salami
sausage
───────────burglar's knapsack contents────────────
salami
ham
brawn
greaves
welt
total
total
</pre>
|