百萬英雄

面試記錄

你參加一個遊戲,在你面前有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))

 

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