import random
def getinput():
a = float(input("请输入队伍A的能力值:"))
b = float(input("请输入队伍B的能力值:"))
n = int(input("模拟比赛的次数:"))
return a, b, n
def simulate_game(a, b):
fa = 0
fb = 0
faqiu="A"
while max(fa, fb) < 15 or abs(fa - fb) < 2:
r = random.random()
if faqiu=="A":
if r <= a:
fa += 1
else:
fb += 1
faqiu="B"
else :
if r<=b:
fb +=1
else:
fa +=1
faqiu="A"
return fa, fb
def getprocess(a, b, n):
wa = 0
wb = 0
for _ in range(n):
wins_a = 0
wins_b = 0
for _ in range(4):
fa, fb = simulate_game(a, b)
if fa > fb:
wins_a += 1
else:
wins_b += 1
if wins_a == 3:
wa += 1
elif wins_b == 3:
wb += 1
else:
fa, fb = simulate_game(a, b)
if fa > fb:
wa += 1
else:
wb += 1
return wa, wb
a, b, n = getinput()
wa, wb = getprocess(a, b, n)
print(f"队伍A赢了 {wa} 次,队伍B赢了 {wb} 次。")
模拟比赛排球
最新推荐文章于 2024-08-15 01:55:01 发布