排球体育规则
print("05")
from random import random
def printInfo(): # 打印程序介绍信息
print('这个程序模拟两个选手A和B的某种竞技比赛')
print('程序运行需要A和B的能力值(以0到1之间的小数表示)')
def getInputs(): # 获得程序运行参数
a = eval(input('请输入选手A的能力值(0-1):'))
b = eval(input('请输入选手B的能力值(0-1):'))
n = eval(input('模拟比赛场次:'))
return a, b, n
def simOneGame(probA, probB, final): # 进行一场比赛
scoreA, scoreB = 0, 0 # 初始化AB的得分
serving = 'A' # 首先由A发球
while not gameOver(scoreA, scoreB, final): # 用while循环来执行比赛
if serving == 'A':
if random() < probA: # random() 方法返回随机生成的一个实数,它在[0,1)范围内。
scoreA += 1 # 用随机数来和能力值比较从而分出胜负
else:
serving = 'B'
else:
if random() < probB:
scoreB += 1
else:
serving = 'A'
return scoreA, scoreB
def simNGames(n, probA, probB): # 进行N场比赛
winsA, winsB = 0, 0 # 初始化AB的胜场数
for i in range(n):
winsOneA, winsOneB = 0, 0
for j in range(4): #前四局
scoreA, scoreB = simOneGame(probA, probB, False)
if scoreA > scoreB:
winsOneA += 1
else:
winsOneB += 1
scoreA, scoreB = simOneGame(probA, probB, True) #决胜局
if scoreA > scoreB:
winsOneA += 1
else:
winsOneB += 1
if winsOneA > winsOneB: #判断一大局谁获胜
winsA += 1
else:
winsB += 1
return winsA, winsB
def gameOver(scoreA, scoreB, final): # 比赛结束
if final is False: #前四局
return scoreA == 15 or scoreB == 15
elif final is True: #决胜局
if (scoreA >= 15 and abs(scoreA - scoreB) >= 2) or (scoreB >= 15 and abs(scoreB - scoreA) >= 2):
return True
def printSummary(n, winA, winB): # 打印比赛结果
print('竞技分析开始,共模拟{}场比赛'.format(n))
print('选手A获胜{}场比赛,占比{:.2f}%'.format(winA, winA / n * 100))
print('选手B获胜{}场比赛,占比{:.2f}%'.format(winB, winB / n * 100))
def main():
printInfo()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(n, winsA, winsB)
main()