鱼C论坛

 找回密码
 立即注册
查看: 3249|回复: 3

[已解决]求大神看看哪里错了,openai非说没错,但是结果根本不对

[复制链接]
发表于 2023-2-9 17:14:55 | 显示全部楼层 |阅读模式

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

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

x
import random
number = int(input("请输入抛硬币次数"))
tail = 0
head = 0
ctail,chead,mhead,mtail,_ = 0, 0, 0,0,0
for i in range(number):
    x = random.randint(1,2)
    if x == 1:
        if number <= 100:
            print("正", end = " ")
        head += 1
        if _ != 2:
            chead += 1
            if chead > mhead:
                mhead = chead
        else:
            chead = 0
        _ = 1
   
    else:
        if number <= 100:
            print("反", end = " ")
        tail += 1
        if _ != 1:
            ctail += 1
            if ctail > mtail:
                mtail = ctail
        else:
            ctail = 0
        _ = 2
        
print(f"正面出现{head}次, 反面出现{tail}次,最多连续正面{mhead}次,最多连续反面{mtail}次")
最佳答案
2023-2-9 17:43:39
看着不舒服,用代码格式发一下再注释说明一下ctail,chead,mtail,mhead,_的意思可能有人会来帮你
如果我对你的代码理解有误请指出
  1. import random
  2. number = int(input("请输入抛硬币次数"))
  3. tail = 0
  4. head = 0
  5. # ctail,chead:本次连续次数
  6. # mtail,mhead:最大连续次数
  7. #_:状态码
  8. ctail,chead,mhead,mtail,_ = 0, 0, 0,0,0
  9. for i in range(number):
  10.     x = random.randint(1,2)
  11.     if x == 1:
  12.         if number <= 100:
  13.             print("正", end = " ")
  14.         head += 1
  15.         if _ != 2:
  16.             chead += 1
  17.             if chead > mhead:
  18.                 mhead = chead
  19.         else:
  20.             chead = 0
  21.         _ = 1
  22.    
  23.     else:
  24.         if number <= 100:
  25.             print("反", end = " ")
  26.         tail += 1
  27.         if _ != 1:
  28.             ctail += 1
  29.             if ctail > mtail:
  30.                 mtail = ctail
  31.         else:
  32.             ctail = 0
  33.         _ = 2
  34.         
  35. print(f"正面出现{head}次, 反面出现{tail}次,最多连续正面{mhead}次,最多连续反面{mtail}次")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-2-9 17:43:39 | 显示全部楼层    本楼为最佳答案   
看着不舒服,用代码格式发一下再注释说明一下ctail,chead,mtail,mhead,_的意思可能有人会来帮你
如果我对你的代码理解有误请指出
  1. import random
  2. number = int(input("请输入抛硬币次数"))
  3. tail = 0
  4. head = 0
  5. # ctail,chead:本次连续次数
  6. # mtail,mhead:最大连续次数
  7. #_:状态码
  8. ctail,chead,mhead,mtail,_ = 0, 0, 0,0,0
  9. for i in range(number):
  10.     x = random.randint(1,2)
  11.     if x == 1:
  12.         if number <= 100:
  13.             print("正", end = " ")
  14.         head += 1
  15.         if _ != 2:
  16.             chead += 1
  17.             if chead > mhead:
  18.                 mhead = chead
  19.         else:
  20.             chead = 0
  21.         _ = 1
  22.    
  23.     else:
  24.         if number <= 100:
  25.             print("反", end = " ")
  26.         tail += 1
  27.         if _ != 1:
  28.             ctail += 1
  29.             if ctail > mtail:
  30.                 mtail = ctail
  31.         else:
  32.             ctail = 0
  33.         _ = 2
  34.         
  35. print(f"正面出现{head}次, 反面出现{tail}次,最多连续正面{mhead}次,最多连续反面{mtail}次")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-9 18:08:44 | 显示全部楼层
  1. import random
  2. head , tail , ctail , chead , mhead , mtail , n = 0 , 0 , 0 , 0 , 0 , 0 , 0
  3. number = int(input("请输入抛硬币次数 : "))
  4. for i in range(number):
  5.     x = random . randint(1 , 2)
  6.     if x != n:
  7.         if x == 1:
  8.             if tail > mtail : mtail = tail
  9.             tail = 0
  10.         else:
  11.             if head > mhead : mhead = head
  12.             head = 0
  13.     if x == 1:
  14.         chead , head = chead + 1 ,  head + 1
  15.     else:
  16.         ctail , tail = ctail + 1 , tail + 1
  17.     if number <= 100:
  18.         print("正", end = " ") if x == 1 else print("反", end = " ")
  19.     n = x
  20. print()
  21. print(f"正面出现{chead}次, 反面出现{ctail}次,最多连续正面{mhead}次,最多连续反面{mtail}次")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 01:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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