A
Google spuckte das hier aus:
///////////////////////////////////////////////////////////////////////////////
// This program demonstrates the mathematic problem commonly known as
// "ziegenproblem".
//
// 16.1.2003 by Jonas Kley <jonas (at) kley.ch>
///////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <strings.h>
#include <iostream>
using namespace std;
// CONSTANTS
const long NUMDOORS = 3; // Number of doors (just works properly with 3)
const long ITERATIONS = 100000; // Update output after ... iterations
// STRUCTS AND TYPES
struct GameCounter
{
long won, lost, num;
};
enum DoorContent {car, zonk} Doors[NUMDOORS];
// FUNCTIONS
void randomize()
///////////////////////////////////////////////////////////////////////////////
// Initialize random-numbers-generator on time (different rand-values...)
{
time_t *zeit = new time_t;
srand(time(zeit));
delete zeit;
}
long RandomDoor()
///////////////////////////////////////////////////////////////////////////////
// Returns a random door.
{
return (rand() % NUMDOORS);
}
long GetBlankDoor(long NotThatOne)
///////////////////////////////////////////////////////////////////////////////
// Returns a door containing a zonk and not that one specified as argument.
{
long result;
do {
result = RandomDoor();
} while ((result == NotThatOne) || (Doors[result] == car));
return result;
}
long GetAnotherDoor(long NotThisOne, long NotThatOne)
///////////////////////////////////////////////////////////////////////////////
// Returns any door but not the ones specified in the arguments (now then the 3rd)
{
long result;
do {
result = RandomDoor();
} while ((result == NotThisOne) || (result == NotThatOne));
return result;
}
void main(int argc, char ** argv)
///////////////////////////////////////////////////////////////////////////////
{
// Yes, randomize!!
randomize();
GameCounter Games = {0, 0, 0};
bool PlayWithChange = false;
// If first argument is "y", "j", "yes" or "ja": play with changes!
if (argc > 1)
{
PlayWithChange = ((strcmp(argv[1], "y") == 0) || (strcmp(argv[1], "j") == 0) || (strcmp(argv[1], "yes") == 0) || (strcmp(argv[1], "ja") == 0));
}
long firstGuess, secondGuess, blankDoor;
// Shall we change after knowing behind which door is a zonk?
if (PlayWithChange)
{
cout << "Changing door after showmaster opens blank door." << endl;
}
else
{
cout << "Not changing door after showmaster opens blank door." << endl;
}
cout << "Press Ctrl-C to stop." << endl;
while (true)
{
for (int i=0; i<ITERATIONS; i++)
{
// Create all the zonk's
for (long i=0; i<NUMDOORS; i++)
Doors[i] = zonk;
// Hide a car behind one door
Doors[RandomDoor()] = car;
// Player: Make your first guess!
firstGuess = RandomDoor();
// Showmaster: Open a door not chosen by player and not containing the car
blankDoor = GetBlankDoor(firstGuess);
// If we change, so do it, otherwise let it be!
if (PlayWithChange)
{
secondGuess = GetAnotherDoor(firstGuess, blankDoor);
}
else
{
secondGuess = firstGuess;
}
// Do we have won??
if (Doors[secondGuess] == car)
{
Games.won++;
}
else
{
Games.lost++;
}
Games.num++;
}
// Update output
cout << "Number of wins : " << Games.won << "; (" << ((double)Games.won)/((double)Games.num) << "%)" << endl;
cout << "Number of defeats: " << Games.lost << "; (" << ((double)Games.lost)/((double)Games.num) << "%)" << endl;
cout << endl;
}
}