A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
|
|
@ -0,0 +1,49 @@
|
|||
import std.stdio, std.algorithm, std.string, std.conv;
|
||||
|
||||
struct Item {
|
||||
string name;
|
||||
real amount, value;
|
||||
|
||||
@property real valuePerKG() const pure nothrow {
|
||||
return value / amount;
|
||||
}
|
||||
|
||||
string toString() const /*pure nothrow*/ {
|
||||
return format("%10s %7.2f %7.2f %7.2f",
|
||||
name, amount, value, valuePerKG);
|
||||
}
|
||||
}
|
||||
|
||||
real sum(string field)(in Item[] items) pure nothrow {
|
||||
return reduce!("a + b." ~ field)(0.0L, items);
|
||||
}
|
||||
|
||||
void main() {
|
||||
Item[] raw = [{"beef", 3.8, 36.0},
|
||||
{"pork", 5.4, 43.0},
|
||||
{"ham", 3.6, 90.0},
|
||||
{"greaves", 2.4, 45.0},
|
||||
{"flitch", 4.0, 30.0},
|
||||
{"brawn", 2.5, 56.0},
|
||||
{"welt", 3.7, 67.0},
|
||||
{"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();
|
||||
|
||||
const(Item)[] chosen;
|
||||
real space = 15.0;
|
||||
foreach (item; items)
|
||||
if (item.amount < space) {
|
||||
chosen ~= item;
|
||||
space -= item.amount;
|
||||
} else {
|
||||
chosen ~= Item(item.name, space, item.valuePerKG * space);
|
||||
break;
|
||||
}
|
||||
|
||||
writefln("%10s %7s %7s %7s", "ITEM", "AMOUNT", "VALUE", "$/unit");
|
||||
writefln("%(%s\n%)", chosen);
|
||||
writeln(Item("TOTAL", sum!"amount"(chosen), sum!"value"(chosen)));
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import std.stdio, std.algorithm;
|
||||
|
||||
void main() {
|
||||
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),
|
||||
T("greaves", 2.4, 45.0),
|
||||
T("flitch", 4.0, 30.0),
|
||||
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);
|
||||
|
||||
auto left = 15.0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue