Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -2,8 +2,8 @@ import std.stdio, std.algorithm, std.typecons, std.array, std.range;
|
|||
|
||||
struct Item { string name; int weight, value; }
|
||||
|
||||
Item[] knapsack01DinamicProgramming(in Item[] items, in int limit)
|
||||
pure nothrow {
|
||||
Item[] knapsack01DinamicProgramming(immutable Item[] items, in int limit)
|
||||
pure nothrow @safe {
|
||||
auto tab = new int[][](items.length + 1, limit + 1);
|
||||
|
||||
foreach (immutable i, immutable it; items)
|
||||
|
|
@ -22,7 +22,7 @@ pure nothrow {
|
|||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
void main() @safe {
|
||||
enum int limit = 400;
|
||||
immutable Item[] items = [
|
||||
{"apple", 39, 40}, {"banana", 27, 60},
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ immutable Item[] items = [
|
|||
struct Solution { uint bits; int value; }
|
||||
static assert(items.length <= Solution.bits.sizeof * 8);
|
||||
|
||||
void solve(in int weight, in int idx, ref Solution s) pure nothrow {
|
||||
void solve(in int weight, in int idx, ref Solution s)
|
||||
pure nothrow @nogc @safe {
|
||||
if (idx < 0) {
|
||||
s.bits = s.value = 0;
|
||||
return;
|
||||
|
|
@ -38,8 +39,9 @@ void solve(in int weight, in int idx, ref Solution s) pure nothrow {
|
|||
s = (v1.value >= v2.value) ? v1 : v2;
|
||||
}
|
||||
|
||||
void main() {
|
||||
void main() @safe {
|
||||
import std.stdio;
|
||||
|
||||
auto s = Solution(0, 0);
|
||||
solve(400, items.length - 1, s);
|
||||
|
||||
|
|
@ -50,5 +52,5 @@ void main() {
|
|||
writeln(" ", it.name);
|
||||
w += it.weight;
|
||||
}
|
||||
writeln("\nTotal value: %d; weight: %d", s.value, w);
|
||||
writefln("\nTotal value: %d; weight: %d", s.value, w);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue