Anonymous user
Knapsack problem/Continuous: Difference between revisions
Updated first D entry
(Added Erlang) |
(Updated first D entry) |
||
Line 338:
=={{header|D}}==
<lang d>import std.stdio, std.algorithm, std.string
struct Item {
Line 344:
real amount, value;
@property real valuePerKG() @safe const pure nothrow {
return value / amount;
}
Line 354:
}
real
return reduce!("a + b." ~ field)(0.0L, items);
}
void main() {
.schwartzSort!(it => -it.valuePerKG)
immutable(Item)[] chosen;
real space = 15.0;
foreach (const item; items)
if (item.amount < space) {
chosen ~= item;
Line 385 ⟶ 384:
writefln("%10s %7s %7s %7s", "ITEM", "AMOUNT", "VALUE", "$/unit");
writefln("%(%s\n%)", chosen);
Item("TOTAL", chosen.
}</lang>
{{out}}
|