zoukankan      html  css  js  c++  java
  • 预测球队比赛成绩

     模拟体育竞技分析

    .模拟乒乓球竞赛

    1.乒乓球比赛规则:

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

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

    from random import random
    def printIntro(): #声明函数,对整个程序功能进行解释
    print("这个程序模拟两个选手A和B的某种竞技比赛")
    print("程序运行需要A和B的能力值(以0到1之间的小数表示)——Author's number:33")
    def getInputs(): #输入函数,输入选手队伍的能力值以及所模拟进行的比赛场次数量
    a = eval(input("请输入选手A的能力值(0-1): "))
    b = eval(input("请输入选手B的能力值(0-1): "))
    n = eval(input("模拟比赛的场次: "))
    return a, b, n
    def simNGames(n, probA, probB): #统计对每一场模拟比赛的结果
    winsA, winsB = 0, 0
    for i in range(n):
    scoreA, scoreB = simOneGame(probA, probB)
    if scoreA > scoreB:
    winsA += 1
    else:
    winsB += 1
    return winsA, winsB
    def gameOver(a,b): #判断比赛结束条件
    return a==15 or b==15
    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获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
    print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))
    def main(): #调用上方功能函数的主函数
    printIntro()
    probA, probB, n = getInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)
    main()

     

  • 相关阅读:
    nginx的配置
    html转pdf
    Mac 安装mysql
    网络编程
    五分钟搞定迭代器生成器
    mac 上多版本python 共存
    一张图片教会你写mysql 语句
    清晰明了的深浅拷贝
    已经入了vim的坑
    解决pip安装太慢的问题
  • 原文地址:https://www.cnblogs.com/155722-lq/p/12964802.html
Copyright © 2011-2022 走看看