鱼C论坛

 找回密码
 立即注册
查看: 2654|回复: 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,_的意思可能有人会来帮你
如果我对你的代码理解有误请指出
import random
number = int(input("请输入抛硬币次数"))
tail = 0
head = 0
# ctail,chead:本次连续次数
# mtail,mhead:最大连续次数
#_:状态码
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}次")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-9 17:43:39 | 显示全部楼层    本楼为最佳答案   
看着不舒服,用代码格式发一下再注释说明一下ctail,chead,mtail,mhead,_的意思可能有人会来帮你
如果我对你的代码理解有误请指出
import random
number = int(input("请输入抛硬币次数"))
tail = 0
head = 0
# ctail,chead:本次连续次数
# mtail,mhead:最大连续次数
#_:状态码
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}次")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-2-9 18:08:44 | 显示全部楼层
import random
head , tail , ctail , chead , mhead , mtail , n = 0 , 0 , 0 , 0 , 0 , 0 , 0 
number = int(input("请输入抛硬币次数 : "))
for i in range(number):
    x = random . randint(1 , 2)
    if x != n:
        if x == 1:
            if tail > mtail : mtail = tail
            tail = 0
        else:
            if head > mhead : mhead = head
            head = 0
    if x == 1:
        chead , head = chead + 1 ,  head + 1
    else:
        ctail , tail = ctail + 1 , tail + 1
    if number <= 100:
        print("正", end = " ") if x == 1 else print("反", end = " ")
    n = x
print()
print(f"正面出现{chead}次, 反面出现{ctail}次,最多连续正面{mhead}次,最多连续反面{mtail}次")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-24 17:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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