预测队伍比赛成绩
1 # -*- coding: utf-8 -*-
2 """
3 Created on Sun May 12 12:26:37 2019
4
5 @author: Regan_White_Lin12
6 """
7
8 from random import random
9 def printIntro():
10 print("这个程序模拟四个选手A,B,C和D的乒乓球比赛")
11 print("程序运行需要A,B,C和D的能力值(以0到1之间的小数表示)")
12 print("作者:Regan_White_Lin 12")
13 def getInputs():
14 a = eval(input("请输入选手A的能力值(0-1): "))
15 b = eval(input("请输入选手B的能力值(0-1): "))
16 c = eval(input("请输入选手C的能力值(0-1): "))
17 d = eval(input("请输入选手D的能力值(0-1): "))
18 n = eval(input("模拟每两名选手比赛的场次: "))
19 return a, b, c, d, n
20 def simNGames(probA, probB):
21 winsA, winsB = 0, 0
22 for i in range(7):
23 scoreA, scoreB = simOneGame(probA, probB)
24 if scoreA > scoreB:
25 winsA += 1
26 else:
27 winsB += 1
28 return winsA, winsB
29 def simnGames1(n,probA,probB):
30 WinsA,WinsB=0,0
31 for i in range(n):
32 winsA, winsB = simNGames(probA,probB)
33 if winsA>winsB:
34 WinsA+=1
35 else:
36 WinsB+=1
37 return WinsA,WinsB
38 def simnGames2(n,probA,probB,WinsA,WinsB):
39 for i in range(n):
40 winsA, winsB = simNGames(probA,probB)
41 if winsA>winsB:
42 WinsA+=1
43 else:
44 WinsB+=1
45 return WinsA,WinsB
46 def gameOver(a,b):
47 if a<11 and b<11:
48 return 0
49 elif a==11 and b<10 or b==11 and a<10:
50 return 1
51 elif a-b==2 and a>11 or b-a==2 and b>11:
52 return 1
53 def simOneGame(probA, probB):
54 scoreA, scoreB = 0, 0
55 serving = "A"
56 while not gameOver(scoreA, scoreB):
57 if serving == "A":
58 if random() < probA:
59 scoreA += 1
60 else:
61 serving="B"
62 else:
63 if random() < probB:
64 scoreB += 1
65 else:
66 serving="A"
67 return scoreA, scoreB
68
69 import operator
70 Departs = []
71 class Department:
72 def __init__(self,num,char):
73 self.num = num大佬们都在玩{精选官网网址: www.vip333.Co }值得信任的品牌平台!
74 self.char = char
75 def main():
76 printIntro()
77 probA, probB, probC, probD, n = getInputs()
78 WinsA, WinsB = simnGames1(n, probA, probB)
79 WinsC, WinsD = simnGames1(n, probC, probD)大佬们都在玩{精选官网网址: www.vip333.Co }值得信任的品牌平台!
80 WinsA, WinsC = simnGames2(n, probA, probC, WinsA, WinsC)
81 WinsB, WinsD = simnGames2(n, probB, probD, WinsB, WinsD)
82 WinsA, WinsD = simnGames2(n, probA, probD, WinsA, WinsD)
83 WinsB, WinsC = simnGames2(n, probB, probC, WinsB, WinsC)
84 print("竞技分析开始,共模拟{}场比赛".format(n*6))
85 Departs.append(Department(WinsA, 'A' ))
86 Departs.append(Department(WinsB, 'B' ))
87 Departs.append(Department(WinsC, 'C' ))
88 Departs.append(Department(WinsD, 'D' ))
89 cmpfun = operator.attrgetter('num')
90 Departs.sort(key=cmpfun)
91 i=0
92 for depart in Departs:
93 i+=1
94 if i==1:
95 print("第四名:选手"+depart.char+",胜局数:"+str(depart.num))
96 elif i==2:
97 print("第三名:选手"+depart.char+",胜局数:"+str(depart.num))
98 elif i==3:大佬们都在玩{精选官网网址: www.vip333.Co }值得信任的品牌平台!
99 print("第二名:选手"+depart.char+",胜局数:"+str(depart.num))
100 elif i==4:
101 print("第一名:选手"+depart.char+",胜局数:"+str(depart.num))
102 main()
103 input("按下任意键退出程序......")
版权声明
本文仅代表作者观点,不代表xx立场。
本文系作者授权xx发表,未经许可,不得转载。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。