C ++ simulation of an IQ problem

zhaozj2021-02-12  143

C simulation of an IQ problem

problem:

You play games in a TV show. This game is a car. The host shows you three doors. He told you that there is a door in the three doors, and the other is the sheep. He let you Select one door. Then you choose a door from the middle, but this door is not open. Then the host opens the left in the two doors is the sheep (because the host knows which door is the door. After the car). Then he said that you still have the opportunity to change your mind. Do you want to change your door or want to choose the door that you originally selected?

What should you do?

Solution 1: Do not change, all the chances

Don't linked to the situation, just consider the current situation, the choice time is to open 1 door, and the situation after opening a door is:

There is only 2 doors, one of them has a car, and the other is sheep, now chooses

In fact, you have chosen and no choice, the situation is 1 car, 1 goat, 50%.

Solution 2:

I must change it!!!

Exaggerated point:

There is a prize in 100W boxes.

You choose one

Others have opened 100W-2 and found that they are empty.

Will you change?

Anyway, if you have prizes, you will decide when you first choose. This chance is very small .....

This is almost certain that there is a prize in the last box !!

It seems that two kinds of answers are reasonable, and the game process is simulated with C , see which one is more losses that do not change.

The idea of ​​simulation is to represent N does with an array of N INT data, where only one value of 1 is 1 car, and other all 0 means all sheep. The user selects one of the elements, then tells the user, except for the selected element and another element of our random choice, other all sheep, give the user a chance to choose again, then make statistics, how many sheep getting after playing X times Know which solution is right.

Below is the program.

The source program is as follows

#include #include #include #include #define max 3 // the Total DOORS MAX

INT DOORS [MAX]; // Max DOORS

Void init () {srand (unsigned)); for (int i = 0; i

INT main (int Argc, char * argv []) {int Times = 0; int Cars = 0; int sheep = 0;

INT i = 0;

INT choose = 0; // the first choose int choose2 = 0;

INT preserved [2]; // the preserved 2 unoided DOORS

While (1) // loop playing {/ cout << "You Play Total:"

COUT << "now there IS" << max << "DOORS, 1 of the door is car," << max-1 << "of the doors are sheep, please choise one" << endl; cout << " Enter Your Choise: "; cin >> choose;

For (i = 0; i

Preserved [1] = choise; // Keep The Other Which User Chose While (preserved [1] == preserved [0]) // if the user chose the door this with car {srand (unsigned (Time (0)) ); preserved [1] = (int (rand ())% max); // randomize another door unopen} cout << endl; cout << "now" <"<< preserved [1] < <"(Which You Chose) and door" << preserved [0] << "(unknown what inside) Are unidened, all the other doors areot, and there is all sheep, i give ua chance to change your mind" < > choose2;

Char s [128]; if (choose2 == 0) {struct (s, "you stick");} else {structure (s, "you change"); cout << "OK, u change your choise to door" << ((choose == preserved [0])? Preserved [1]: preserved [0]) << Endl; choose = (choose == preserved [0])? Preserved [1]: preserved [0];}

COUT << Endl; cout << "<" << S << ", now i tell you the car is in door" << preserved [0] << Endl; if (choos == preserved [0]) {cout << "YOU GET A Car" << ENDL; CARS ;} else {cout << "You get a sheep" << Endl; Sheep ;} /// Times ;} returnography

///

On the top

#define max 3

The number of definitions, first like topics, it is more difficult to see the difference between change and no change, but when the number of doors gradually increases (such as 100), if you play 10 times or 20 times or more If you choose not to change, it is almost always getting a sheep.

This shows that the answer 2 is right, that is, the host gives you the opportunity to choose, must change! !

转载请注明原文地址:https://www.9cbs.com/read-6797.html

New Post(0)