Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
53
Task/100-prisoners/D/100-prisoners.d
Normal file
53
Task/100-prisoners/D/100-prisoners.d
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import std.array;
|
||||
import std.random;
|
||||
import std.range;
|
||||
import std.stdio;
|
||||
import std.traits;
|
||||
|
||||
bool playOptimal() {
|
||||
auto secrets = iota(100).array.randomShuffle();
|
||||
|
||||
prisoner:
|
||||
foreach (p; 0..100) {
|
||||
auto choice = p;
|
||||
foreach (_; 0..50) {
|
||||
if (secrets[choice] == p) continue prisoner;
|
||||
choice = secrets[choice];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool playRandom() {
|
||||
auto secrets = iota(100).array.randomShuffle();
|
||||
|
||||
prisoner:
|
||||
foreach (p; 0..100) {
|
||||
auto choices = iota(100).array.randomShuffle();
|
||||
foreach (i; 0..50) {
|
||||
if (choices[i] == p) continue prisoner;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
double exec(const size_t n, bool function() play) {
|
||||
size_t success = 0;
|
||||
for (int i = n; i > 0; i--) {
|
||||
if (play()) {
|
||||
success++;
|
||||
}
|
||||
}
|
||||
return 100.0 * success / n;
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum N = 1_000_000;
|
||||
writeln("# of executions: ", N);
|
||||
writefln("Optimal play success rate: %11.8f%%", exec(N, &playOptimal));
|
||||
writefln(" Random play success rate: %11.8f%%", exec(N, &playRandom));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue