鱼C论坛

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

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

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

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

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

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

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

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

  5. white = [32, 35, 40, 52, 54]
  6. red = 1
  7. print("Your selection for white balls are: ", *mywhite )
  8. print("Your selection for red balls is: ", myred )
  9. print("Winning numbers for white balls:", *white)
  10. print("Winning number for red ball:", red)
  11. matchwhite = 0
  12. for i, j in zip(mywhite, white):
  13.     if i == j:
  14.         matchwhite += 1
  15. matchred = 1 if myred == red else 0
  16. if matchwhite == 0 and matchred ==0:
  17.     print ("No matching. Thanks for your contribution!")
  18. elif matchred:
  19.     if matchwhite == 0:
  20.         print ("Matching only the red ball: $4")
  21.     elif matchwhite == 1:
  22.         print ("Matching the red ball and one white ball: $4")
  23.     elif matchwhite == 2:
  24.         print ("Matching the red ball and two white balls: $7")
  25.     elif matchwhite == 3:
  26.         print ("Matching the red ball and three white balls: $100")
  27.     elif matchwhite == 4:
  28.         print ("Matching the red ball and four white balls: $10,000")
  29.     else:
  30.         print ("Matching five white balls and the red ball: Jackpot")
  31. else:
  32.     if matchwhite == 3:
  33.         print ("Matching three white balls: $7")
  34.     elif matchwhite == 4:
  35.         print ("Matching four white balls: $100")
  36.     else:
  37.         print ("Matching five white balls: $1,000,000")


复制代码

求大佬能帮我解答一下,感激不尽!!
最佳答案
2021-9-12 19:10:14
  1. import random

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

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

  16.     myWhite = set(myWhite)
  17.     if myRed == red:
  18.         return len(myWhite & white) >= 3
  19.         # if not (myWhite & white):
  20.         #     print("Matching only the red ball: $4")
  21.         # elif len(myWhite & white) == 1:
  22.         #     print("Matching the red ball and one white ball: $4")
  23.         # elif len(myWhite & white) == 2:
  24.         #     print("Matching the red ball and two white balls: $7")
  25.         # elif len(myWhite & white) == 3:
  26.         #     print("Matching the red ball and three white balls: $100")
  27.         # elif len(myWhite & white) == 4:
  28.         #     print("Matching the red ball and four white balls: $10,000")
  29.         # elif len(myWhite & white) == 5:
  30.         #     print("Matching five white balls and the red ball: Jackpot")
  31.     elif myRed != red:
  32.         return False
  33.         # if not (myWhite & white):
  34.         #     print("No matching. Thanks for your contribution!")
  35.         # elif len(myWhite & white) == 3:
  36.         #     print("Matching three white balls: $7")
  37.         # elif len(myWhite & white) == 4:
  38.         #     print("Matching four white balls: $100")
  39.         # elif len(myWhite & white) == 5:
  40.         #     print("Matching five white balls: $1,000,000")
  41.    
  42. if __name__ == "__main__":
  43.     # print(game.__doc__)
  44.     myWhite = [32, 35, 40, 52, 54]
  45.     myRed = 1
  46.     count = 0
  47.     while not game(myWhite, myRed):
  48.         count += 1
  49.     print(count)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

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

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

  16.     myWhite = set(myWhite)
  17.     if myRed == red:
  18.         return len(myWhite & white) >= 3
  19.         # if not (myWhite & white):
  20.         #     print("Matching only the red ball: $4")
  21.         # elif len(myWhite & white) == 1:
  22.         #     print("Matching the red ball and one white ball: $4")
  23.         # elif len(myWhite & white) == 2:
  24.         #     print("Matching the red ball and two white balls: $7")
  25.         # elif len(myWhite & white) == 3:
  26.         #     print("Matching the red ball and three white balls: $100")
  27.         # elif len(myWhite & white) == 4:
  28.         #     print("Matching the red ball and four white balls: $10,000")
  29.         # elif len(myWhite & white) == 5:
  30.         #     print("Matching five white balls and the red ball: Jackpot")
  31.     elif myRed != red:
  32.         return False
  33.         # if not (myWhite & white):
  34.         #     print("No matching. Thanks for your contribution!")
  35.         # elif len(myWhite & white) == 3:
  36.         #     print("Matching three white balls: $7")
  37.         # elif len(myWhite & white) == 4:
  38.         #     print("Matching four white balls: $100")
  39.         # elif len(myWhite & white) == 5:
  40.         #     print("Matching five white balls: $1,000,000")
  41.    
  42. if __name__ == "__main__":
  43.     # print(game.__doc__)
  44.     myWhite = [32, 35, 40, 52, 54]
  45.     myRed = 1
  46.     count = 0
  47.     while not game(myWhite, myRed):
  48.         count += 1
  49.     print(count)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

哈哈哈,这是一个题啊,只是附加的问题。
我如果想买彩票,我就会问全中该怎么算了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

使用道具 举报

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

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

使用道具 举报

 楼主| 发表于 2021-9-12 20:30:29 | 显示全部楼层
我运行了一下你的代码,但是为什么其它的都不显示,只显示一个次数啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-13 07:13:51 | 显示全部楼层
盲猜应该买 69*68*67 = 314364 张
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-18 16:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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