|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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}次")
看着不舒服,用代码格式发一下再注释说明一下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}次")
复制代码
|
|