#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
void Swap(int *a, int *b)
{
int iTmp = *a;
*a = *b;
*b = iTmp;
}
bool GuessWithNoSwitch(void)
{
static int arrInt[3] = { 0, 0, 1 };
for (int iIdx = 0; iIdx < 3; ++iIdx)
{
const int iRandom1 = rand() % 3;
const int iRandom2 = rand() % 3;
Swap(&arrInt[iRandom1], &arrInt[iRandom2]);
}
const int iRandom3 = rand() % 3;
if (0 == arrInt[(iRandom3 + 1) % 3])
{
}
else if (0 == arrInt[(iRandom3 + 2) % 3])
{
}
bool bCheck = false;
if (1 == arrInt[iRandom3])
{
bCheck = true;
}
return bCheck;
}
bool GuessWithSwitch(void)
{
static int arrInt[3] = { 0, 0, 1 };
for (int iIdx = 0; iIdx < 3; ++iIdx)
{
const int iRandom1 = rand() % 3;
const int iRandom2 = rand() % 3;
Swap(&arrInt[iRandom1], &arrInt[iRandom2]);
}
int iRandom3 = rand() % 3;
if (0 == arrInt[(iRandom3 + 1) % 3])
{
iRandom3 = (iRandom3 + 2) % 3;
}
else if (0 == arrInt[(iRandom3 + 2) % 3])
{
iRandom3 = (iRandom3 + 1) % 3;
}
bool bCheck = false;
if (1 == arrInt[iRandom3])
{
bCheck = true;
}
return bCheck;
}
int main(int argc, char* argv[])
{
srand((unsigned int)time(NULL));
printf("The first case: No witch choice!\n");
const int iMaxCount = (int)1e5;
int iCntNoSwitch = 0;
for (int iIdx = 0; iIdx < iMaxCount; ++iIdx)
{
if (GuessWithNoSwitch())
{
++iCntNoSwitch;
}
}
const double dRatio1 = 1.0 * iCntNoSwitch / iMaxCount;
printf("case 1 ratio: %lf\n", dRatio1);
printf("The second case: Witch choice!\n");
int iCntSwitch = 0;
for (int iIdx = 0; iIdx < iMaxCount; ++iIdx)
{
if (GuessWithSwitch())
{
++iCntSwitch;
}
}
const double dRatio2 = 1.0 * iCntSwitch / iMaxCount;
printf("case 1 ratio: %lf\n", dRatio2);
return 0;
}