Knapsack problem/Continuous: Difference between revisions
Content added Content deleted
m (added links to ;Related tasks:) |
|||
Line 2,199: | Line 2,199: | ||
greaves 2.40 45.00 |
greaves 2.40 45.00 |
||
welt 3.50 63.38 |
welt 3.50 63.38 |
||
</pre> |
|||
=={{header|Phix}}== |
|||
<lang Phix>constant meats = { |
|||
--Item Weight (kg) Price (Value) |
|||
{"beef", 3.8, 36}, |
|||
{"pork", 5.4, 43}, |
|||
{"ham", 3.6, 90}, |
|||
{"greaves", 2.4, 45}, |
|||
{"flitch", 4.0, 30}, |
|||
{"brawn", 2.5, 56}, |
|||
{"welt", 3.7, 67}, |
|||
{"salami", 3.0, 95}, |
|||
{"sausage", 5.9, 98}} |
|||
function by_weighted_value(integer i, j) |
|||
atom {?,weighti,pricei} = meats[i], |
|||
{?,weightj,pricej} = meats[j] |
|||
return compare(pricej/weightj,pricei/weighti) |
|||
end function |
|||
sequence tags = custom_sort(routine_id("by_weighted_value"),tagset(length(meats))) |
|||
atom w = 15, worth = 0 |
|||
for i=1 to length(tags) do |
|||
object {desc,wi,price} = meats[tags[i]] |
|||
atom c = min(wi,w) |
|||
printf(1,"%3.1fkg%s of %s\n",{c,iff(c=wi?" (all)":""),desc}) |
|||
worth += (c/wi)*price |
|||
w -= c |
|||
if w=0 then exit end if |
|||
end for |
|||
printf(1,"Total value: %f\n",{worth})</lang> |
|||
{{out}} |
|||
<pre> |
|||
3.0kg (all) of salami |
|||
3.6kg (all) of ham |
|||
2.5kg (all) of brawn |
|||
2.4kg (all) of greaves |
|||
3.5kg of welt |
|||
Total value: 349.378378 |
|||
</pre> |
</pre> |
||
Line 2,242: | Line 2,283: | ||
welt 3.50 63.38 |
welt 3.50 63.38 |
||
15.00 349.38</pre> |
15.00 349.38</pre> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<lang pli>*process source xref attributes; |
<lang pli>*process source xref attributes; |