球队预测
作者:互联网
from random import random
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): # 进行决赛
scoreA, scoreB =0, 0
serving = 'A'
while not gameOver(scoreA, scoreB):
if serving == 'A':
if random() > probA:
scoreB += 1
serving = 'B'
else:
scoreA += 1
else:
if random() > probB:
scoreA += 1
serving = 'A'
else:
scoreB += 1
return scoreA, scoreB def simfirstgame(probA, probB):
scoreA, scoreB = 0, 0
for i in range(4):
s1, s2=0, 0
while not gameover(s1, s2):
if random()<probA:
s1+=1
elif random()<probB:
s2+=1
if s1>s2:
scoreA+=1
else:
scoreB+=1
return scoreA, scoreB def simNGames(n, probA, probB): #进行N场比赛
winsA, winsB = 0, 0 # 初始化AB的胜场数
for i in range(n):
k, l=simfirstgame(probA, probB)
if k==1:
winsB+=1
continue
elif k==3:
winsA+=1
continue
scoreA, scoreB = simOneGame(probA, probB)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
return winsA, winsB def gameOver(c, d): #比赛结束
return (c>=15 and c-d>=2) or (d>=15 and d-c>=2)
def gameover(scoreA, scoreB):
return (scoreA>=25 and scoreA-scoreB>=2) or (scoreB>=25 and scoreB-scoreA>=2)
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():
printIntro()
probA, probB, n =getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(n, winsA, winsB) main() def GameOver(N,scoreA,scoreB):
if N<=4:
return(scoreA>=25 and scoreB>=25 and abs(scoreA-scoreB)>=2)
else:
return(scoreA>=15 and abs(scoreA-scoreB)>=2) or (scoreB>=15 and abs(scoreA-scoreB)>=2)
ai=[]
bi=[]
try:
for scoreA,scoreB in ((1,25),(1,26),(25,25),(16,17),(28,30)):
if GameOver(scoreA,scoreB):
ai.append(scoreA)
bi.append(scoreB)
except:
print('Error') print(ai)
print(bi)
a = eval(input('队伍A的能力值(0-1):'))
b = eval(input('队伍B的能力值(0-1):'))
n = eval(input('模拟比赛场次:'))
return a, b, n def simOneGame(probA, probB): # 进行决赛
scoreA, scoreB =0, 0
serving = 'A'
while not gameOver(scoreA, scoreB):
if serving == 'A':
if random() > probA:
scoreB += 1
serving = 'B'
else:
scoreA += 1
else:
if random() > probB:
scoreA += 1
serving = 'A'
else:
scoreB += 1
return scoreA, scoreB def simfirstgame(probA, probB):
scoreA, scoreB = 0, 0
for i in range(4):
s1, s2=0, 0
while not gameover(s1, s2):
if random()<probA:
s1+=1
elif random()<probB:
s2+=1
if s1>s2:
scoreA+=1
else:
scoreB+=1
return scoreA, scoreB def simNGames(n, probA, probB): #进行N场比赛
winsA, winsB = 0, 0 # 初始化AB的胜场数
for i in range(n):
k, l=simfirstgame(probA, probB)
if k==1:
winsB+=1
continue
elif k==3:
winsA+=1
continue
scoreA, scoreB = simOneGame(probA, probB)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
return winsA, winsB def gameOver(c, d): #比赛结束
return (c>=15 and c-d>=2) or (d>=15 and d-c>=2)
def gameover(scoreA, scoreB):
return (scoreA>=25 and scoreA-scoreB>=2) or (scoreB>=25 and scoreB-scoreA>=2)
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():
printIntro()
probA, probB, n =getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(n, winsA, winsB) main() def GameOver(N,scoreA,scoreB):
if N<=4:
return(scoreA>=25 and scoreB>=25 and abs(scoreA-scoreB)>=2)
else:
return(scoreA>=15 and abs(scoreA-scoreB)>=2) or (scoreB>=15 and abs(scoreA-scoreB)>=2)
ai=[]
bi=[]
try:
for scoreA,scoreB in ((1,25),(1,26),(25,25),(16,17),(28,30)):
if GameOver(scoreA,scoreB):
ai.append(scoreA)
bi.append(scoreB)
except:
print('Error') print(ai)
print(bi)
标签:return,预测,scoreB,probB,probA,球队,def,scoreA 来源: https://www.cnblogs.com/study007/p/13179041.html