百万英雄

面试记录

你参加一个游戏,在你面前有4张1000万支票,其中一张是真的。游戏开始,你选了一张,之后主持人在剩下
的3张里,选择一个展示出来,验证后发现是假的。

问题:请分情况理性分析,此时,你的参赛权的价格

  • 情况一:不允许修改之前的选择
  • 情况二:有重新选择的权利

回答:请用下面两种方法分别作答

  • 方式1(理论推导):请给出理论推导和计算过程,情况二需说明如何行使权力;
  • 方式2(编程模拟):使用程序准确客观地模拟上述两种情况下,选手平均获得的奖金,得到参赛权的价格

参考资料:

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))

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章