• 模拟乒乓球双打和单打比赛


    规则:单打七局四胜,双打五局三胜。单局:先得11分者胜;若11平,则先多得2分者胜。

    乒乓球赛单打预测:

    from random import random
    def printIntro():
        print("这个程序模拟两个选手A和B的某种比赛")
        print("程序运行需要A和B的能力值(0到1之间)")
    def getInputs():
        a=eval(input("请输入选手A的能力值(0-1):"))
        b=eval(input("请输入选手B的能力值(0-1):"))
        return a,b
    def simNGames(probA,probB):
        winsA,winsB=0,0
        for i in range(7):
            scoreA,scoreB=simOneGame(probA,probB)
            if scoreA>scoreB:
                winsA+=1
                if winsA==4:
                    break
            else:
                winsB+=1
                if winsB==4:
                    break
        return winsA,winsB
    def gameOver(a,b):
        return a==11 or b==11
    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获胜{}场比赛,占比{:.1%}".format(winsA,winsA/n))
        print("选手B获胜{}场比赛,占比{:.1%}".format(winsB,winsB/n))
    def main():
        printIntro()
        probA,probB = 0.5,0.5
        winsA,winsB = simNGames(probA,probB)
        printSummary(winsA,winsB)
    print("043林树锋的结果截图")

    main()

    结果:

    乒乓球赛双打预测

    from random import random
    def printIntro():

    print(
    "这个程序模拟两个选手A和B的某种比赛") print("程序运行需要A和B的能力值(0到1之间)") def getInputs(): a=eval(input("请输入选手A的能力值(0-1):")) b=eval(input("请输入选手B的能力值(0-1):")) return a,b def simNGames(probA,probB): winsA,winsB=0,0 for i in range(5): scoreA,scoreB=simOneGame(probA,probB) if scoreA>scoreB: winsA+=1 if winsA==3: break else: winsB+=1 if winsB==3: break return winsA,winsB def gameOver(a,b): return a==11 or b==11 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获胜{}场比赛,占比{:.1%}".format(winsA,winsA/n)) print("选手B获胜{}场比赛,占比{:.1%}".format(winsB,winsB/n)) def main(): printIntro() probA,probB = 0.5,0.5 winsA,winsB = simNGames(probA,probB) printSummary(winsA,winsB)

    main()

    结果:

  • 相关阅读:
    ubuntu 解决“无法获得锁 /var/lib/dpkg/lock -open (11:资源暂时不可用)”的方法
    解决VMware安装ubuntu16.04后无法全屏的问题
    力推:无限制下载神器aria2
    使用Pangolon在同一副图中,画出两个轨迹,比较误差
    Pangolin中opengl的混合(gl_blend)
    Ubuntu 16.04: How to install OpenCV
    ubuntu16.04下安装Eigen
    ubuntu16.04下安装Sophus
    svn删除账户信息
    javaweb乱码(tomcat服务器)
  • 原文地址:https://www.cnblogs.com/2640335699qqcom/p/12883225.html
Copyright © 2020-2023  润新知