鱼C论坛

 找回密码
 立即注册
查看: 1151|回复: 8

[已解决]概率问题,小白求解

[复制链接]
发表于 2021-9-12 18:21:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Ddaniel1177 于 2021-9-12 18:30 编辑

买彩票,然后问至少要买多少张才能至少中三个白球。
这是我目前的代码
import random

print("Buy a ticket with 5 white balls [not identical, between 1-69] and 1 red ball [between 1-26]")
mywhite = random.sample(range(1, 69), 5)
myred = random.randint(1, 26)

white = [32, 35, 40, 52, 54]
red = 1
print("Your selection for white balls are: ", *mywhite )
print("Your selection for red balls is: ", myred )
print("Winning numbers for white balls:", *white)
print("Winning number for red ball:", red)
matchwhite = 0
for i, j in zip(mywhite, white):
    if i == j:
        matchwhite += 1
matchred = 1 if myred == red else 0
if matchwhite == 0 and matchred ==0:
    print ("No matching. Thanks for your contribution!")
elif matchred:
    if matchwhite == 0:
        print ("Matching only the red ball: $4")
    elif matchwhite == 1:
        print ("Matching the red ball and one white ball: $4")
    elif matchwhite == 2:
        print ("Matching the red ball and two white balls: $7")
    elif matchwhite == 3:
        print ("Matching the red ball and three white balls: $100")
    elif matchwhite == 4:
        print ("Matching the red ball and four white balls: $10,000")
    else:
        print ("Matching five white balls and the red ball: Jackpot")
else:
    if matchwhite == 3:
        print ("Matching three white balls: $7")
    elif matchwhite == 4:
        print ("Matching four white balls: $100")
    else:
        print ("Matching five white balls: $1,000,000")
求大佬能帮我解答一下,感激不尽!!
最佳答案
2021-9-12 19:10:14
import random

def game(myWhite, myRed):
    """
    Buy a ticket with 5 white balls [not identical, between 1-69]
    and 1 red ball [between 1-26]
    """
    white = set(random.sample(range(1, 70), 5))
    red = random.choice(range(1, 27))
    # string = {1: "first", 2: "second", 3: "third", 4: "forth", 5: "fiveth"}
    # myWhite = [int(input(f"Your {string[i]} number for white balls\n")) for i in range(1, 6)]
    # myRed = int(input("Your number for red ball\n"))

    # print("Your selection for white balls are: ", *myWhite )
    # print("Your selection for red balls is: ", myRed )
    # print("Winning numbers for white balls:", *white)
    # print("Winning number for red ball:", red)

    myWhite = set(myWhite)
    if myRed == red:
        return len(myWhite & white) >= 3
        # if not (myWhite & white):
        #     print("Matching only the red ball: $4")
        # elif len(myWhite & white) == 1:
        #     print("Matching the red ball and one white ball: $4")
        # elif len(myWhite & white) == 2:
        #     print("Matching the red ball and two white balls: $7")
        # elif len(myWhite & white) == 3:
        #     print("Matching the red ball and three white balls: $100")
        # elif len(myWhite & white) == 4:
        #     print("Matching the red ball and four white balls: $10,000")
        # elif len(myWhite & white) == 5:
        #     print("Matching five white balls and the red ball: Jackpot")
    elif myRed != red:
        return False
        # if not (myWhite & white):
        #     print("No matching. Thanks for your contribution!")
        # elif len(myWhite & white) == 3:
        #     print("Matching three white balls: $7")
        # elif len(myWhite & white) == 4:
        #     print("Matching four white balls: $100")
        # elif len(myWhite & white) == 5:
        #     print("Matching five white balls: $1,000,000")
    
if __name__ == "__main__":
    # print(game.__doc__)
    myWhite = [32, 35, 40, 52, 54]
    myRed = 1
    count = 0
    while not game(myWhite, myRed):
        count += 1
    print(count)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-9-12 19:10:14 | 显示全部楼层    本楼为最佳答案   
import random

def game(myWhite, myRed):
    """
    Buy a ticket with 5 white balls [not identical, between 1-69]
    and 1 red ball [between 1-26]
    """
    white = set(random.sample(range(1, 70), 5))
    red = random.choice(range(1, 27))
    # string = {1: "first", 2: "second", 3: "third", 4: "forth", 5: "fiveth"}
    # myWhite = [int(input(f"Your {string[i]} number for white balls\n")) for i in range(1, 6)]
    # myRed = int(input("Your number for red ball\n"))

    # print("Your selection for white balls are: ", *myWhite )
    # print("Your selection for red balls is: ", myRed )
    # print("Winning numbers for white balls:", *white)
    # print("Winning number for red ball:", red)

    myWhite = set(myWhite)
    if myRed == red:
        return len(myWhite & white) >= 3
        # if not (myWhite & white):
        #     print("Matching only the red ball: $4")
        # elif len(myWhite & white) == 1:
        #     print("Matching the red ball and one white ball: $4")
        # elif len(myWhite & white) == 2:
        #     print("Matching the red ball and two white balls: $7")
        # elif len(myWhite & white) == 3:
        #     print("Matching the red ball and three white balls: $100")
        # elif len(myWhite & white) == 4:
        #     print("Matching the red ball and four white balls: $10,000")
        # elif len(myWhite & white) == 5:
        #     print("Matching five white balls and the red ball: Jackpot")
    elif myRed != red:
        return False
        # if not (myWhite & white):
        #     print("No matching. Thanks for your contribution!")
        # elif len(myWhite & white) == 3:
        #     print("Matching three white balls: $7")
        # elif len(myWhite & white) == 4:
        #     print("Matching four white balls: $100")
        # elif len(myWhite & white) == 5:
        #     print("Matching five white balls: $1,000,000")
    
if __name__ == "__main__":
    # print(game.__doc__)
    myWhite = [32, 35, 40, 52, 54]
    myRed = 1
    count = 0
    while not game(myWhite, myRed):
        count += 1
    print(count)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-12 19:22:00 | 显示全部楼层
你該不會是想要用代碼運算答案去買樂透吧?兄弟,這和你當初題目設定不一樣哦,那些沒有中獎的不用打印?只算出次數?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-12 19:34:27 | 显示全部楼层
傻眼貓咪 发表于 2021-9-12 19:22
你該不會是想要用代碼運算答案去買樂透吧?兄弟,這和你當初題目設定不一樣哦,那些沒有中獎的不用 ...

哈哈哈,这是一个题啊,只是附加的问题。
我如果想买彩票,我就会问全中该怎么算了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-12 19:45:39 | 显示全部楼层
傻眼貓咪 发表于 2021-9-12 19:22
你該不會是想要用代碼運算答案去買樂透吧?兄弟,這和你當初題目設定不一樣哦,那些沒有中獎的不用 ...

大佬,为啥我把你代码的最后一段放进我的里面,它就开始一直循环啊。难道是要出现三次中3个白球的情况才停下来吗?有什么方法可以直接得出结果吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-12 20:02:43 | 显示全部楼层
Ddaniel1177 发表于 2021-9-12 19:45
大佬,为啥我把你代码的最后一段放进我的里面,它就开始一直循环啊。难道是要出现三次中3个白球的情况才 ...

1. 用我的代碼就不會出現無限循環,你把我的後半段代碼放進別人的前半段代碼當然有問題咯,看來我的代碼並不是你想要的啊。
2. 你用我的代碼你會發現,只求中獎三顆白球以上的機率不高啊,幸運可能 300 次以內,否則上千上萬次數,你確定全部結果要打印?
不好意思兄弟,我的 Python 技術不是很厲害,你的題目我無法解了,你看看有沒有其他大神可以幫助你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-12 20:30:29 | 显示全部楼层
我运行了一下你的代码,但是为什么其它的都不显示,只显示一个次数啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-12 22:05:09 | 显示全部楼层
Ddaniel1177 发表于 2021-9-12 20:30
我运行了一下你的代码,但是为什么其它的都不显示,只显示一个次数啊

你上面要求中獎三顆白色球的次數啊,不是嗎?

兄弟你先想清楚,你真正想要什麼,問題必須要有邏輯,有條有理,不然別人很難解決你的問題啊~
大家的代碼只是參考,你必須要了解其原理啊,然後試試自己動手寫吧

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-13 07:13:51 | 显示全部楼层
盲猜应该买 69*68*67 = 314364 张
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-13 10:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表