规则:单打七局四胜,双打五局三胜。单局:先得11分者胜;若11平,则先多得2分者胜。
乒乓球赛单打预测:
from random import random def printIntro(): print("这个程序模拟两个选手A和B的某种比赛") print("程序运行需要A和B的能力值(0到1之间)") def getInputs(): a=eval(input("请输入选手A的能力值(0-1):")) b=eval(input("请输入选手B的能力值(0-1):")) return a,b def simNGames(probA,probB): winsA,winsB=0,0 for i in range(7): scoreA,scoreB=simOneGame(probA,probB) if scoreA>scoreB: winsA+=1 if winsA==4: break else: winsB+=1 if winsB==4: break return winsA,winsB def gameOver(a,b): return a==11 or b==11 def simOneGame(probA,probB): scoreA,scoreB = 0,0 serving = 'A' while not gameOver(scoreA,scoreB): if serving == 'A': if random()<probA: scoreA+=1 else: serving='B' else: if random()<probB: scoreB+=1 else: serving='A' return scoreA,scoreB def printSummary(winsA,winsB): n=winsA+winsB print("竞技分析开始,共模拟{}场比赛".format(n)) print("选手A获胜{}场比赛,占比{:.1%}".format(winsA,winsA/n)) print("选手B获胜{}场比赛,占比{:.1%}".format(winsB,winsB/n)) def main(): printIntro() probA,probB = 0.5,0.5 winsA,winsB = simNGames(probA,probB) printSummary(winsA,winsB)
print("043林树锋的结果截图")
main()
结果:
乒乓球赛双打预测:
from random import random
def printIntro():
print("这个程序模拟两个选手A和B的某种比赛") print("程序运行需要A和B的能力值(0到1之间)") def getInputs(): a=eval(input("请输入选手A的能力值(0-1):")) b=eval(input("请输入选手B的能力值(0-1):")) return a,b def simNGames(probA,probB): winsA,winsB=0,0 for i in range(5): scoreA,scoreB=simOneGame(probA,probB) if scoreA>scoreB: winsA+=1 if winsA==3: break else: winsB+=1 if winsB==3: break return winsA,winsB def gameOver(a,b): return a==11 or b==11 def simOneGame(probA,probB): scoreA,scoreB = 0,0 serving = 'A' while not gameOver(scoreA,scoreB): if serving == 'A': if random()<probA: scoreA+=1 else: serving='B' else: if random()<probB: scoreB+=1 else: serving='A' return scoreA,scoreB def printSummary(winsA,winsB): n=winsA+winsB print("竞技分析开始,共模拟{}场比赛".format(n)) print("选手A获胜{}场比赛,占比{:.1%}".format(winsA,winsA/n)) print("选手B获胜{}场比赛,占比{:.1%}".format(winsB,winsB/n)) def main(): printIntro() probA,probB = 0.5,0.5 winsA,winsB = simNGames(probA,probB) printSummary(winsA,winsB)
main()
结果: