zoukankan      html  css  js  c++  java
  • 排球比赛——预测

    赛制规定

    前4局比赛采用5局3胜制,每个队只有赢得至少25分,饼统摄超过对方2分时,才胜1局。

    正式比赛采用5局3胜制,决胜局的比赛采用15分制,一队先得8分后,两队交换场区,按照原位置顺序继续比赛到结束。

    在决胜局(第五局)之比赛,先获得15分并领先对方2分为胜利。

     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)
    View Code

    结果如下

  • 相关阅读:
    Python基础笔记(五)
    Python基础笔记(四)
    Python基础笔记(三)
    Python基础笔记(二)
    Python基础笔记(一)
    分页存储过程
    MD Test
    vue路由的配置技巧
    Echarts的使用与配置项
    js中call,apply,bind之间的区别
  • 原文地址:https://www.cnblogs.com/xugama/p/12883523.html
Copyright © 2011-2022 走看看