Knapsack problem/Unbounded: Difference between revisions

m
→‎displays all solutions: added whitespace, used a template for the output section.
m (added background color to the (top) title columns.)
m (→‎displays all solutions: added whitespace, used a template for the output section.)
Line 3,381:
<lang rexx>/*REXX program solves the knapsack/unbounded problem: highest value, weight, and volume.*/
 
maxPanacea= 0
maxPanacea=0 /* value weight volume */
maxIchor = 0; /* ═══════ value weight ═══════ ══════volume */
maxGold = 0; /* ═══════ ══════ ══════ */
maxGold =0; panacea.$ = 3000 ; panacea.w = 0.3 ; panacea.v = 0.025
max$ = 0; ichorpanacea.$ = 18003000 ; ichor panacea.w = 0.2 3 ; ichor panacea.v = 0.015025
now. = 0; goldichor.$ = 25001800 ; gold ichor.w = 0.2 ; ; goldichor.v = 0.002015
# = 0; sack gold.$ = 2500 ; 0 ; sack gold.w = 25 2 ; sack gold.v = 0.25002
maxPanacea=0L = 0; /* value sack.$ = 0 ; weight sack.w = 25 ; volume */ sack.v = 0.25
L =0
 
maxPanacea= min(sack.w / panacea.w, sack.v / panacea.v)
maxIchor = min(sack.w / ichor.w, sack.v / ichor.v)
Line 3,398 ⟶ 3,399:
now.w = g * gold.w + i * ichor.w + p * panacea.w
now.v = g * gold.v + i * ichor.v + p * panacea.v
if now.w > sack.w | now.v > sack.v then iterate i
if now.$ > max$ then do; #= 0; max$= now.$; end
if now.$ = max$ then do; #= # + 1; maxP.#= p; maxI.#= i; maxG.#= g
max$.#= now.$; maxW.#= now.w; maxV.#= now.v
L= max(L, length(p + i + g) )
end
end /*g (gold) */
end /*i (ichor) */
end /*p (panacea)*/
L= L + 1
do j=1 for #; say; say copies('▒', 70) "solution" j
say ' panacea in sack:' right(maxP.j, L)
say ' ichors in sack:' right(maxI.j, L)
say ' gold items in sack:' right(maxG.j, L)
say '════════════════════' copies("═", L)
say 'carrying a total of:' right(maxP.j + maxI.j + maxG.j, L)
say left('', 40) "total value: " max$.j / 1
say left('', 40) "total weight: " maxW.j / 1
say left('', 40) "total volume: " maxV.j / 1
end /*j*/
/*stick a fork in it, we're all done. */</lang>
{{out|output|text=&nbsp; when using the internal default input:}}
'''output'''
<pre>
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ solution 1