• 预测球队比赛成绩


    一. 模拟体育竞技分析

    1.体育竞技分析程序

    (1)模拟体育竞技程序,我们采用自顶向下的设计方法。自顶向下设计中最重要的是顶层设计。以体育体育竞技分析为例,可以从问题的IPO描述开始。大多数程序都可以讲IPO描述直接用到程序设计结构中。体育竞技分析从用户处得到模拟参数,最后呢输出结果。下面是一个基础设计的步骤。

    步骤一:打印程序的介绍信息。

    步骤二:获得程序运行的参数, 即probA,probB,n。

    步骤三:利用球员的能力值。模拟n场比赛。

    步骤四:输出球员的获胜比赛场次及概率。 

    (2)自顶向下设计的基本思想,如下图:

    2. 体育竞技分析的IPO模式:

    输入I(input):两个球员的能力值,模拟比赛的次数(其中,运动员的能力值,可以通过发球方赢得本回合的概率来表示,

                一个能力值为0.8的球员,在他发球时,有80%的可能性赢得1分)

    处理P(process):模拟比赛过程

    输出O(output):两个球员获胜的概率

    二.模拟乒乓球竞赛

    1.乒乓球比赛规则:

    (1)一局比赛:
    在一局比赛中,先得11分的一方为胜方;10平后,先多得2分的一方为胜方。
    (2)一场比赛:
    单打的淘汰赛采用七局四胜制,双打淘汰赛和团体赛采用五局三胜制。

    (3)模拟乒乓球比赛的完整代码 

     

    import random 
    from math import *
    def printIntro():#打印程序的介绍性信息
        print("这个程序模拟量个选手A和B的某种竞技比赛")
        print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
        print("editor:02豆芽运气")
     
    def getInputs():#获得用户输入的参数
        a = eval(input("请输入选手A的能力值(0-1): "))
        b = eval(input("请输入选手B的能力值(0-1): "))
        h= eval(input("请输入一场要打几局:"))
        n = eval(input("模拟比赛的场次: "))
        return a, b, h,n
     
    def printSummary(winsA, winsB):
        n = winsA + winsB
        print("竞技分析开始, 共模拟{}场比赛".format(n))
        print("选手A获胜{}场比赛, 占比{:0.1%}".format(winsA, winsA/n))
        print("选手B获胜{}场比赛, 占比{:0.1%}".format(winsB, winsB/n))
    def gameOver(scoreA, scoreB):
         g=scoreA-scoreB
        if (abs(g)==2 and  scoreA> 10 and scoreB> 10) or (g> 0 and scoreA==11 and scoreB!=10) or (g<0 and scoreB==11 and soreA!=10):
            return True
        else:
            return False
        
    def simOneGame(probA, probB,h):#模拟一场比赛
        for i in range(h): #模拟七局四胜或五局三胜为一场
            serving = "A"
            roundA =roundB=0  #分别为队伍A和B的赢得的比赛的局次
            scoreA, scoreB = 0, 0
            while not gameOver(scoreA, scoreB): #模拟一局比赛
                roundA=roundB=0
                if serving == "A":
                    if random.random() < probA:
                        scoreA += 1             
                    else:
                        serving = "B"
                else:
                    if random.random() < probB:
                        scoreB += 1
                  
                    else:
                        serving = "A"
            if scoreA>scoreB:
                roundA += 1
            else:
                roundB += 1
        return roundA,roundB
        
     
    def simNGames(n ,probA, probB,h):#利用A,B的的能力值模拟N场比赛
        winsA, winsB = 0, 0
        for i in range(n):
            roundA , roundB = simOneGame(probA, probB,h)
            if roundA >roundB:
                winsA += 1
            else:
                winsB += 1
        return winsA, winsB
     
    def main():
        printIntro()
        probA, probB, h,n = getInputs()#分别为队伍A和B的能力值,一场的局数,比赛的场次
        winsA, winsB = simNGames(n, probA, probB,h)#分别为队伍A和B的赢得的比赛的场次
        printSummary(winsA, winsB)
        if h==5:
            print('此次模拟单打淘汰赛')
        else:
            print('此次模拟双打淘汰赛或者是团体淘汰赛')
    main() 

    执行代码的效果

    三。模拟足球竞赛

     1.比赛规则:采用回合制。一名球员在一个回合中未能合法击打时,回合结束。本回合胜方得一分、得球权。先得15分赢得一场比赛。

    模拟的代码如下

    # -*- coding: utf-8 -*-
    """
    Created on Tue May 14 18:28:58 2019
    
    @author: 02豆芽运气
    """
    
    from random import random
    
    def printInfo():    # 打印程序介绍信息
        print('这个程序模拟两个选手A和B的某种竞技比赛')
        print('程序运行需要A和B的能力值(以0到1之间的小数表示')
        print('editor:豆芽运气')
    
    def getInputs():    # 获得程序运行参数
        a = eval(input('请输入选手A的能力值(0-1):'))
        b = eval(input('请输入选手B的能力值(0-1):'))
        n = eval(input('模拟比赛场次:'))
        return a, b, n
    
    def simOneGame(probA, probB):    # 进行一场比赛
        scoreA, scoreB = 0, 0   # 初始化AB的得分
        serving = 'A'         # 首先由A发球
        while not gameOver(scoreA, scoreB):  #用while循环来执行比赛
            if serving == 'A':
                if random() < probA:   # random() 方法返回随机生成的一个实数,它在[0,1)范围内。
                    scoreA += 1     # 用随机数来和能力值比较从而分出胜负
                else:
                    serving = 'B'
            else:
                if random() < probB:
                    scoreB += 1
                else:
                    serving = 'A'
        return scoreA, scoreB
    
    def simNGames(n, probA, probB):    #进行N场比赛
        winsA, winsB = 0, 0    # 初始化AB的胜场数
        for i in range(n):
            scoreA, scoreB = simOneGame(probA, probB)
            if scoreA > scoreB:
                winsA += 1
            else:
                winsB += 1
        return winsA, winsB
    
    def gameOver(c, d):    #比赛结束
        return c==15 or d==15
    
    def printSummary(n ,winA, winB):    #打印比赛结果
        print('竞技分析开始,共模拟{}场比赛'.format(n))
        print('选手A获胜{}场比赛,占比{:.2f}%'.format(winA, winA/n*100))
        print('选手B获胜{}场比赛,占比{:.2f}%'.format(winB, winB / n * 100))
    def main():
        printInfo()
        probA, probB, n =getInputs()
        winsA, winsB = simNGames(n, probA, probB)
        printSummary(n, winsA, winsB)
    
    main()

     执行结果

     到这里就结束了。

  • 相关阅读:
    Mybatis实现简单的数据库增删改查操作
    MySQL压缩版安装配置教程
    ClassLoader简单教程
    Javascript Duff装置 循环展开(Javascript Loop unrolling Duff device)
    Visual Studio warning MSB3270:There was a mismatch between the processor architecture of the project being built "MSIL"
    Register/unregister a dll to GAC
    sign a third-party dll which don't have a strong name
    SQL Server(SSIS package) call .net DLL
    the convertion between string and BlobColumn
    bulk insert data into database with table type .net
  • 原文地址:https://www.cnblogs.com/2987831760qq-com/p/10848556.html
Copyright © 2020-2023  润新知