标签:mon while 测试 core alt mat 规则 over too
def printInfo(): print("这个程序模拟两个选手A和B的羽毛球竞技比赛") print("程序需要两个选手的能力值0-1") print("规则:三局两胜--21分制") printInfo()
代码如下:
1 def printSummary(winsA,winsB):
2
3 n = winsA + winsB
4
5 print("竞技分析开始,共模拟{}场比赛".format(n))
6
7 print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n))
8
9 print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n))
10
11 winA=eval(input())
12 winB=eval(input())
13 printSummary(winA,winB)
结果如下:
因为原来的函数要调用simOneGame()函数,所以我就改了一下函数,直接用probA和probB来代替scoreA和scoreB,即用能力值来代替每一局的分数。
代码如下:
def simNGames(m,n,probA,probB):
winsA,winsB = 0,0
wa,wb=0,0
for i in range(n):
for i in range(m):
#scoreA,scoreB = simOneGame(probA,probB)
scoreA,scoreB=probA,probB
if scoreA > scoreB:
wa += 1
else:
wb += 1
if wa==2:
winsA+=1
wa,wb=0,0
break
if wb==2:
winsB+=1
wa,wb=0,0
break
return winsA,winsB
m=eval(input())
n=eval(input())
probA=eval(input())
probB=eval(input())
print(simNGames(m,n,probA,probB))
结果如下:
代码如下:
1 from random import random
2 def simOneGame(probA,probB):
3
4 scoreA,scoreB = 0,0
5
6 serving = "A"
7 while not gameOver(scoreA,scoreB):
8
9 if serving == "A":
10
11 if random() < probA:
12
13 scoreA += 1
14 else:
15 serving = "B"
16 else:
17 if random() < probB:
18 scoreB += 1
19 else:
20 serving = "A"
21 return scoreA,scoreB
22
23 def gameOver(a,b):
24 if(a>=20 or b>=20):
25 if(abs(a-b)==2 and a<=29 and b<=29):
26 return True
27 else:
28 return a==30 or b==30
29 else:
30 return False
31
32 #for i in range(10):
33 a=eval(input())
34 b=eval(input())
35 print(gameOver(a,b))
36
37 probA=eval(input())
38 probB=eval(input())
39 print(simOneGame(probA,probB))
结果如下:
标签:mon while 测试 core alt mat 规则 over too
原文地址:https://www.cnblogs.com/MARCOREUS11/p/12883091.html