标签:需要 lse += tle rand eve 分析 code span
乒乓球比赛规则:
代码:
1 from random import random 2 def printIntro(): 3 print("兵乓球比赛结果预测") 4 def getInputs(): 5 a = eval(input("请输入选手A的能力值(0-1): ")) 6 b = eval(input("请输入选手B的能力值(0-1): ")) 7 x = eval(input("模拟比赛的场次: ")) 8 return a, b, x 9 10 def simNGames(x, probA, probB): 11 winsA, winsB = 0, 0 12 for i in range(x): 13 scoreA, scoreB = simOneGame(probA, probB) 14 print(scoreA,scoreB) 15 if scoreA > scoreB: 16 winsA += 1 17 else: 18 winsB += 1 19 return winsA, winsB 20 def simOneGame(probA, probB): 21 scoreA, scoreB = 0, 0 22 serving = "A" 23 while not gameOver(scoreA, scoreB): 24 if serving == "A": 25 if random() < probA: 26 scoreA += 1 27 else: 28 serving="B" 29 else: 30 if random() < probB: 31 scoreB += 1 32 else: 33 serving="A" 34 35 return scoreA, scoreB 36 def gameOver(a,b): 37 if (a>=11 and abs(a-b)>=2) or (b>=11 and abs(a-b)>=2): 38 return True 39 40 def printSummary(winsA, winsB): 41 x = winsA + winsB 42 print("竞技分析开始,共模拟{}场比赛".format(x)) 43 print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/x)) 44 print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/x)) 45 def main(): 46 printIntro() 47 probA, probB, x = getInputs() 48 winsA, winsB = simNGames(x, probA, probB) 49 printSummary(winsA, winsB) 50 main()
结果:
标签:需要 lse += tle rand eve 分析 code span
原文地址:https://www.cnblogs.com/tantan0914/p/12884406.html