賽制規定app
前4局比賽採用5局3勝制,每一個隊只有贏得至少25分,餅統攝超過對方2分時,才勝1局。dom
正式比賽採用5局3勝制,決勝局的比賽採用15分制,一隊先得8分後,兩隊交換場區,按照原位置順序繼續比賽到結束。ide
在決勝局(第五局)之比賽,先得到15分並領先對方2分爲勝利。spa
1 from random import random 2 3 def printIntro(): # 打印程序介紹信息 4 print("2019310143026 馬曉嘉") 5 print('這個程序模擬兩個隊伍A和B的排球競技比賽') 6 print('程序運行須要A和B的能力值(以0到1之間的小數表示)') 7 8 def getInputs(): # 得到程序運行參數 9 a = eval(input('請輸入隊伍A的能力值(0-1):')) 10 b = eval(input('請輸入隊伍B的能力值(0-1):')) 11 n = eval(input('模擬比賽場次:')) 12 return a, b, n 13 14 def simOneGame(probA, probB): # 進行決賽 15 scoreA, scoreB =0, 0 16 serving = 'A' 17 while not gameOver(scoreA, scoreB): 18 if serving == 'A': 19 if random() > probA: 20 scoreB += 1 21 serving = 'B' 22 else: 23 scoreA += 1 24 else: 25 if random() > probB: 26 scoreA += 1 27 serving = 'A' 28 else: 29 scoreB += 1 30 return scoreA, scoreB 31 32 def simfirstgame(probA, probB): 33 scoreA, scoreB = 0, 0 34 for i in range(4): 35 s1, s2=0, 0 36 while not gameover(s1, s2): 37 if random()<probA: 38 s1+=1 39 elif random()<probB: 40 s2+=1 41 if s1>s2: 42 scoreA+=1 43 else: 44 scoreB+=1 45 return scoreA, scoreB 46 47 def simNGames(n, probA, probB): #進行N場比賽 48 winsA, winsB = 0, 0 # 初始化AB的勝場數 49 for i in range(n): 50 k, l=simfirstgame(probA, probB) 51 if k==1: 52 winsB+=1 53 continue 54 elif k==3: 55 winsA+=1 56 continue 57 scoreA, scoreB = simOneGame(probA, probB) 58 if scoreA > scoreB: 59 winsA += 1 60 else: 61 winsB += 1 62 return winsA, winsB 63 64 def gameOver(c, d): #比賽結束 65 return (c>=15 and c-d>=2) or (d>=15 and d-c>=2) 66 def gameover(scoreA, scoreB): 67 return (scoreA>=25 and scoreA-scoreB>=2) or (scoreB>=25 and scoreB-scoreA>=2) 68 69 70 def printSummary(n ,winA, winB): #打印比賽結果 71 print('競技分析開始,共模擬{}場比賽'.format(n)) 72 print('隊伍A獲勝{}場比賽,佔比{:.2f}%'.format(winA, winA/n*100)) 73 print('隊伍B獲勝{}場比賽,佔比{:.2f}%'.format(winB, winB / n * 100)) 74 def main(): 75 printIntro() 76 probA, probB, n =getInputs() 77 winsA, winsB = simNGames(n, probA, probB) 78 printSummary(n, winsA, winsB) 79 80 main() 81 82 def GameOver(N,scoreA,scoreB): 83 if N<=4: 84 return(scoreA>=25 and scoreB>=25 and abs(scoreA-scoreB)>=2) 85 else: 86 return(scoreA>=15 and abs(scoreA-scoreB)>=2) or (scoreB>=15 and abs(scoreA-scoreB)>=2) 87 ai=[] 88 bi=[] 89 try: 90 for scoreA,scoreB in ((1,25),(1,26),(25,25),(16,17),(28,30)): 91 if GameOver(scoreA,scoreB): 92 ai.append(scoreA) 93 bi.append(scoreB) 94 except: 95 print('Error') 96 97 print(ai) 98 print(bi)
結果以下code