面试记录你参加一个游戏,在你面前有4张1000万支票,其中一张是真的。游戏开始,你选了一张,之后主持人在剩下 问题:请分情况理性分析,此时,你的参赛权的价格
回答:请用下面两种方法分别作答
|
参考资料:
https://www.cnblogs.com/antineutrino/p/4821580.html
https://www.cnblogs.com/antineutrino/p/4826998.html
思路分析:
逻辑实现:
#知情 不反选 1/4
import random
choices = [False, True, False, False]
####随机获取一个
def rangGetIndex():
return random.randint(0,choices.__len__()-1)
###主持人获取一个,选择一个不中奖的
def hostGetIndex(choices,firstIndex):
'''
for i in range(choices.__len__()):
if (i!= firstIndex and choices[i]==False):
return i
'''
hostIndex = random.randint(0,choices.__len__()-1);
while (hostIndex == firstIndex or choices[hostIndex] == True):
hostIndex = random.randint(0,choices.__len__()-1);
return hostIndex
totalNum = 1000000
getNum = 0
for lop in range(totalNum):
###我先拿一个
firstIndex = rangGetIndex()
firstRes = choices[firstIndex]
###主持人登场
hostIndex = hostGetIndex(choices,firstIndex)
hostRes = choices[hostIndex]
if firstRes:
getNum +=1
print("执行"+str(totalNum)+"次,命中 "+str(getNum)+" 次,概率为"+str(getNum/totalNum))
###知情 反选 ,概率为3/8
import random
###import copy 引入失败 先不解决
####随机获取一个
choices = [False, True, False, False]
def rangGetIndex():
return random.randint(0,choices.__len__()-1)
totalNum = 1000000
getNum = 0
for i in range(totalNum):
##随机获取一个
firstIndex = rangGetIndex()
###临时数组
choicesTemp = [False, True, False, False]
###第一次获取的结果
firstRes = choicesTemp[firstIndex]
###删除临时数组的数据
choicesTemp.pop(firstIndex)
####删除假的数据,从另外三个人删除
choicesTemp.remove(False)
####第二次选择
secondRes = random.choice(choicesTemp)
if secondRes:
getNum +=1
print("执行"+str(totalNum)+"次,命中 "+str(getNum)+" 次,概率为"+str(getNum/totalNum))
#不知情 反选 1/3
import random
####随机获取一个
def rangGetIndex(choices):
return random.randint(0,choices.__len__()-1)
###主持人获取一个
def hostGetIndex(choices,firstIndex):
'''
for i in range(choices.__len__()):
if (i!= firstIndex):
return i
'''
hostIndex = random.randint(0,choices.__len__()-1);
while (hostIndex == firstIndex):
hostIndex = random.randint(0,choices.__len__()-1);
return hostIndex
totalNum = 1000000
getNum = 0
realNum = 0
for lop in range(totalNum):
choices = [False, True, False, False]
###我先拿一个
firstIndex = rangGetIndex(choices)
firstRes = choices[firstIndex]
###主持人登场
hostIndex = hostGetIndex(choices,firstIndex)
hostRes = choices[hostIndex]
###排除中奖的次数
if hostRes:
continue
else:
realNum +=1
####去除 我和主持人拿的
choices.remove(firstRes)
choices.remove(hostRes)
firstRes = random.choice(choices)
if firstRes:
getNum +=1
print("执行"+str(totalNum)+"次,命中 "+str(getNum)+" 次,概率为"+str(getNum/realNum))
#不知情 不反选 1/3
import random
choices = [False, True, False, False]
####随机获取一个
def rangGetIndex():
return random.randint(0,choices.__len__()-1)
###主持人获取一个
def hostGetIndex(choices,firstIndex):
'''for i in range(choices.__len__()):
if (i!= firstIndex):
return i
'''
hostIndex = random.randint(0,choices.__len__()-1);
while (hostIndex == firstIndex):
hostIndex = random.randint(0,choices.__len__()-1);
return hostIndex
totalNum = 1000000
getNum = 0
realNum = 0
for lop in range(totalNum):
###我先拿一个
firstIndex = rangGetIndex()
firstRes = choices[firstIndex]
###主持人登场
hostIndex = hostGetIndex(choices,firstIndex)
hostRes = choices[hostIndex]
###排除中奖的次数
if hostRes:
continue
else:
realNum +=1
if firstRes:
getNum +=1
print("执行"+str(totalNum)+"次,命中 "+str(getNum)+" 次,概率为"+str(getNum/realNum))