模拟乒乓球比赛
Posted qq1294
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟乒乓球比赛相关的知识,希望对你有一定的参考价值。
#乒乓球比赛,单打淘汰与双打 #规则:单打淘汰制,5局3胜。双打7局4胜,先得11分者胜。若双方都为10分,则先到2分者胜 #分俩个队进行,每个队分别有俩个队员。 #1队队名:IG,2队队名:RNG import random as r def printIntro(): print("这个程序模拟四个选手A和B和C和D的某种竞技比赛") print(‘并且设定A与B在1队,C与D在2队‘) print("程序运行需要选手的能力值(以0到1之间的小数表示)") def getInputs(): a = eval(input("请输入选手A的能力值(0-1): ")) b = eval(input("请输入选手B的能力值(0-1): ")) c = eval(input("请输入选手C的能力值(0-1): ")) d = eval(input("请输入选手D的能力值(0-1): ")) n = eval(input("模拟比赛的场次: ")) return a,b,c,d,n def simNGames(n, probA, probB,probC,probD): wins1,wins2=0,0 for i in range(n): scoreA, scoreB = shuangda(probA,probB,probC,probD) if scoreA > scoreB: wins1 += 1 else: wins2 += 1 return wins1, wins2 def gameover(score1,score2,k): if(score1==10 and score2==10): if k==1: score1+=1 if k==2: score2+=1 if(score1==11 or score2==1): return score1,score2 def gameover1(number1,number2): return number1==3 or number2==3 def onevone(prob1,prob2): number1,number2=0,0 serving = "A" while not gameover1(number1,number2): if(number1==2 and number2<2): k=1 if(number1<2 and number2==2): k=2 if serving == "A": if r.random() < prob1: number1 += 1 else: serving="B" else: if r.random() < prob2: number2 += 1 else: serving="A" return number1,number2,k def taotai(probA,probB,probC,probD): score1, score2 ,k,m= 0,0,0,0 number1,number2,k=onevone(probA,probC) score1+=number1 score2+=number2 if score1>score2: number1,number2,m=onevone(probA,probD) score1+=number1 score2+=number2 if number1>number2: score1+=number1 score2+=number2 else: number1,number2,m=onevone(probB,probD) score1+=number1 score2+=number2 else: number1,number2,m=onevone(probB,probC) score1+=number1 score2+=number2 if number1<number2: score1+=number1 score2+=number2 else: number1,number2,m=onevone(probB,probD) score1+=number1 score2+=number2 m=k return score1,score2,m def shuangda(probA,probB,probC,probD): number1,number2=0,0 score1,score2,k=taotai(probA,probB,probC,probD) number1+=score1 number2+=score2 serving = "A" while not gameover(number1,number2,k): if serving == "A": n=r.random() if (n>probA and n>probB): serving="B" else: number1+=1 else: N=r.random() if (N>probC and N>probD): serving="A" else: number2+=1 return number1,number2 def printSummary(winsA, winsB): n = winsA + winsB print("竞技分析开始,共模拟{}场比赛".format(n)) print("1队获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n)) print("2队获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n)) def main(): printIntro() probA, probB, probC, probD, n = getInputs() wins1, wins2 = simNGames(n,probA,probB,probC,probD) printSummary(wins1, wins2) main()
以上是关于模拟乒乓球比赛的主要内容,如果未能解决你的问题,请参考以下文章
两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。 已抽签决定比赛名单。有人向队员打听比赛的名单。 a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。(代码片段