Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/Josephus-problem/D/josephus-problem.d
Normal file
27
Task/Josephus-problem/D/josephus-problem.d
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import std.stdio, std.algorithm, std.array, std.string, std.range;
|
||||
|
||||
T pop(T)(ref T[] items, in size_t i) pure /*nothrow*/ @safe /*@nogc*/ {
|
||||
auto aux = items[i];
|
||||
items = items.remove(i);
|
||||
return aux;
|
||||
}
|
||||
|
||||
string josephus(in int n, in int k) pure /*nothrow*/ @safe {
|
||||
auto p = n.iota.array;
|
||||
int i;
|
||||
immutable(int)[] seq;
|
||||
while (!p.empty) {
|
||||
i = (i + k - 1) % p.length;
|
||||
seq ~= p.pop(i);
|
||||
}
|
||||
|
||||
return format("Prisoner killing order:\n%(%(%d %)\n%)." ~
|
||||
"\nSurvivor: %d",
|
||||
seq[0 .. $ - 1].chunks(20), seq[$ - 1]);
|
||||
}
|
||||
|
||||
void main() /*@safe*/ {
|
||||
josephus(5, 2).writeln;
|
||||
writeln;
|
||||
josephus(41, 3).writeln;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue