马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# 选手发球同时赢得比赛才得一分,发球但输掉比赛则换对手发球
# 先获得15分的选手获胜import random as rd
def printInfo():
print("这个程序模拟两个选手A和B的某种竞技比赛")
print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
def getInputs():
a = eval(input("请输入选手A的能力值(0-1):"))
b = eval(input("请输入选手B的能力值(0-1):"))
n = eval(input("模拟比赛的次数:"))
return a, b, n
def gameOver(scoreA, scoreB):
return scoreA == 15 or scoreB == 15
def simOnegame(probA, probB):
scoreA, scoreB = 0, 0
serving = "A"
while not gameOver(scoreA, scoreB):
if serving == "A":
if rd.random() < probA:
scoreA += 1
else:
serving = "B"
else:
if rd.random() < probB:
scoreB += 1
else:
serving = "A"
return scoreA, scoreB
def simNgames(n, probA, probB):
winA, winB = 0, 0
for i in range(n):
scoreA, scoreB = simOnegame(probA, probB)
if scoreA > scoreB:
winA += 1
else:
winB += 1
return winA, winB
def printSummary(winA, winB):
n = winA + winB
print("竞技分析开始,共模拟%d场比赛" % n)
print("选手A获胜%d场比赛,占比%.3f" % (winA, winA / n))
print("选手B获胜%d场比赛,占比%.3f" % (winB, winB / n))
def main():
printInfo()
probA, probB, n = getInputs()
winA, winB = simNgames(n, probA, probB)
printSummary(winA, winB)
main()
|