This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -29,10 +29,10 @@ void main() {
{"salami", 3.0, 95.0},
{"sausage", 5.9, 98.0}];
// reverse sorted by Value per amount
const items = raw.sort!q{a.valuePerKG > b.valuePerKG}().release();
// Reverse sorted by Value per amount.
const items = raw.sort!q{a.valuePerKG > b.valuePerKG}.release;
const(Item)[] chosen;
immutable(Item)[] chosen;
real space = 15.0;
foreach (item; items)
if (item.amount < space) {
@ -45,5 +45,5 @@ void main() {
writefln("%10s %7s %7s %7s", "ITEM", "AMOUNT", "VALUE", "$/unit");
writefln("%(%s\n%)", chosen);
writeln(Item("TOTAL", sum!"amount"(chosen), sum!"value"(chosen)));
Item("TOTAL", chosen.sum!"amount", chosen.sum!"value").writeln;
}

View file

@ -1,7 +1,8 @@
import std.stdio, std.algorithm;
void main() {
import std.stdio, std.algorithm;
static struct T { string item; double weight, price; }
auto items = [T("beef", 3.8, 36.0),
T("pork", 5.4, 43.0),
T("ham", 3.6, 90.0),
@ -10,19 +11,16 @@ void main() {
T("brawn", 2.5, 56.0),
T("welt", 3.7, 67.0),
T("salami", 3.0, 95.0),
T("sausage", 5.9, 98.0)];
sort!q{a.price/a.weight > b.price/b.weight}(items);
T("sausage", 5.9, 98.0)]
.schwartzSort!q{ -a.price / a.weight };
auto left = 15.0;
foreach (it; items) {
foreach (it; items)
if (it.weight <= left) {
writeln("Take all the ", it.item);
if (it.weight == left)
return;
left -= it.weight;
} else {
writefln("Take %.1fkg %s", left, it.item);
return;
}
}
} else
return writefln("Take %.1fkg %s", left, it.item);
}