首页 > 代码库 > The Monty Hall Problem-蒙特霍问题
The Monty Hall Problem-蒙特霍问题
//The Monty Hall Problem #include <iostream> #include <cstdlib> // Needed for random numbers using namespace std; // ==================== // main function // ==================== int main() { int i; int numWinsStay = 0; int numWinsSwitch = 0; int prizeDoor, choiceDoor, switchDoor, revealDoor; // Run simulation 10,000 times for (i=0; i<10000; i++) { prizeDoor = rand() % 3; // Pick door with prize behind it choiceDoor = rand() % 3; // Door of contestant‘s pick // Find a door to reveal that is not the prize and not the contestant‘s // choice //两个while始终保持choicedoor,switchdoor,revealdoor为三个不同值:0,1,2。 //prizedoor则为其中一个。 revealDoor = 0; while ((revealDoor == prizeDoor) || (revealDoor == choiceDoor)) { revealDoor++; } // Find a door if the contestant switches switchDoor = 0; while ((switchDoor == choiceDoor) || (switchDoor == revealDoor)) { switchDoor++; } // See if we would have won and increment counters if (choiceDoor == prizeDoor) { numWinsStay++; } else if (switchDoor == prizeDoor) { numWinsSwitch++; } } cout << "If you switch, you will win " << (numWinsSwitch / 100) << "%" << " of the time. " << endl; cout << "If you stay, you will win " << (numWinsStay / 100) << "%" << " of the time. " << endl; return 0; }
The Monty Hall Problem
The Monty Hall Problem-蒙特霍问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。