Anonymous user
Knapsack problem/Continuous: Difference between revisions
m
→version 1: added comments, added a variable to specify number of decimal digits in the FORMAT for SAY, removed an unnecessary subroutine, allowed the specification of weight and output precision.
Walterpachl (talk | contribs) m (→version 2: ooRexx highlighting does (of course) not work within the lang section) |
m (→version 1: added comments, added a variable to specify number of decimal digits in the FORMAT for SAY, removed an unnecessary subroutine, allowed the specification of weight and output precision.) |
||
Line 1,916:
=={{header|REXX}}==
===version 1===
Any
<lang rexx>/*REXX program solves the burglar's knapsack (continuous) problem.
@.= /*═══════ name weight value ══════*/
@.1 = 'flitch 4 30 '
@.2 = 'beef 3.8 36 '
@.3 = 'pork 5.4 43 '
@.4 = 'greaves 2.4 45 '
@.5 = 'brawn 2.5 56 '
@.6 = 'welt 3.7 67 '
@.7 = 'ham 3.6 90 '
@.8 = 'salami 3 95 '
@.9 = 'sausage 5.9 98 '
parse arg maxW ddig . /*get possible args from the C.L.*/
if maxW=='' | maxW==',' then maxW=15 /*burglar's knapsack max weight. */
wL=length('weight'); nL=length('total weight'); vL=length(' value ')▼
if ddig=='' | ddig==',' then ddig= 2 /*# of decimal digits in FORMAT. */
▲wL=length('weight'); nL=length('total weight'); vL=length(' value ')
totW=0; totV=0
items=j-1 /*ITEMS
nL=nL + nL%4
wL=max(wL, length(format(totw,,
vL=max(vL, length(format(totv,,
totW=0; totV=0 /*set weight & value totals to 0.*/
call show 'unsorted item list' /*display a header and the @ list*/
do
do k=
kp1=k+1;
end /*k*/
kp1=k+1; n.kp1=_n;
end /*
call hdr "
if
call syf n.j,
else
f=(maxW-totW) /
call syf n.j, w.j*f
end
call sep /* ddig = #
▲ end
call sy left('total value',nL,'─'),
▲call sy left('total weight',nL,'─'), format(totW,,2)
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────one─liner subroutines────────────────────*/
hdr:
sep: call sy copies('═',nL), copies("═",wL), copies('═',vL); return
show: call hdr arg(1); do j=1 for items; call syf n.j,w.j,v.j;end; say; return
sy: say
syf: call sy arg(1), format(arg(2),,
title: call sy center('item',nL),center("weight",wL),center('value',vL); return</lang>
'''output''' using the default inputs of: <tt> 15 2 </tt>
<pre style="height:50ex">
────────────────unsorted item list────────────────
Line 1,995 ⟶ 1,993:
item weight value
|