zoukankan      html  css  js  c++  java
  • 体育竞技模拟(兵兵球)

    比赛规则

    1. 一场比赛: 单打:采用七局四胜制
                双打淘汰赛、团体赛:采用五局三胜制
    2. 一局比赛: 先得11分为胜,10平后,多得2分为胜 3. 一局比赛: 每队发球2次后,接发球方即成为发球方,依此类推,直至该局比赛结束或者到双方比分都达到10分或实行轮换发球法时,发球和接发次序仍然不变,但每人只轮发一分球
    # coding: utf-8
    
    from random import random
    from math import fabs
    
    def printIntro():
        print("这个程序模拟两个选手A和B的某种竞技比赛")
        print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
        print()
    
    def getInputs():
        a = eval(input("请输入选手A的能力值(0-1):"))
        b = eval(input("请输入选手B的能力值(0-1):"))
        n = eval(input("模拟比赛的场次:"))
        print()
        return a, b, 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(a,b):
        if fabs(a-b) > 2 and a > 9 and b > 9:
            return True
        if a == 11 and b < 10:
            return True
        if b == 11 and a < 10:
            return True
    
    def simOneGame(proA,proB):
        scoreA, scoreB = 0, 0
        serving = "A"
        while not gameOver(scoreA,scoreB):
            if serving == "A":
                if random() < proA:
                    scoreA += 1
                else:
                    serving = "B"
            else:
                if random() < proB:
                    scoreB += 1
                else:
                    serving = "A"
        return scoreA, scoreB
        
    def simNGames(n,proA,proB):
        winsA, winsB = 0, 0 
        for i in range(n):
            scoreA, scoreB = simOneGame(proA,proB)
            if scoreA > scoreB:
                winsA += 1
            else:
                winsB += 1
        return winsA, winsB
    
    def main():
        printIntro()
        proA, proB, n = getInputs()
        winsA, winsB = simNGames(n,proA,proB)
        printSummary(winsA,winsB)
        
    main()
  • 相关阅读:
    kindle--瓦尔登湖
    8051
    c++
    Linux安装目录的选择
    Redis键值数据类型之散列类型
    Redis键值数据类型之字符串
    redis基本使用
    Ubuntu18.04 Redis安装
    Java String和int转换
    mysql获得自增主码的值
  • 原文地址:https://www.cnblogs.com/justlikecode/p/10870918.html
Copyright © 2011-2022 走看看