其他分享
首页 > 其他分享> > 乒乓球比赛预测

乒乓球比赛预测

作者:互联网

 1 from random import random
 2 
 3 class bisai(object):
 4     def __init__(self, nlA, nlB, n):
 5         self.nlA = nlA
 6         self.nlB = nlB
 7         self.n = n
 8         self.dfA = 0
 9         self.dfB = 0
10         self.slA = 0
11         self.slB = 0
12         self.scA = 0
13         self.scB = 0
14     def bsai(self):
15         qquan = 'A'
16         for i in range(self.n):
17             #重置比分
18             self.slA = 0
19             self.slB = 0
20             while not self.slA==4 or self.slB==4:
21                 #重置得分
22                 self.dfA = 0
23                 self.dfB = 0
24                 while not self.dfA==11 or self.dfB==11:
25                     if qquan == 'A':
26                         if random()<self.nlA:
27                             self.dfA += 1
28                         else:
29                             qquan = 'B'
30                     else:
31                         if random()<self.nlB:
32                             self.dfB += 1
33                         else:
34                             qquan = 'A'
35 
36                     #先到11分获胜,如果出现10平先多积两分者获胜
37                     if self.dfA >= 11 and self.dfB < 10:
38                         self.slA += 1
39                         break
40                     elif self.dfB >= 11 and self.dfA < 10:
41                         self.slB += 1
42                         break
43                     #出现10平先多积两分者获胜
44                     elif self.dfA == 10 and self.dfB == 10:
45                         while not self.dfA==12 or self.dfB==12:
46                             if qquan == 'A':
47                                 if random()<self.nlA:
48                                     self.dfA += 1
49                                 else:
50                                     qquan = 'B'
51                             else:
52                                 if random()<self.nlB:
53                                     self.dfB += 1
54                                 else:
55                                     qquan = 'A'
56                             if self.dfA >= 12 and self.dfB < 12:
57                                 break
58                             elif self.dfB >= 12 and self.dfA < 12:
59                                 break
60                     #先积两分者获胜
61                     if self.dfA >= 12 and self.dfB < 12:
62                         self.slA += 1
63                         break
64                     elif self.dfB >= 12 and self.dfA < 12:
65                         self.slB += 1
66                         break
67                 #七局四胜
68                 if self.slA >= 4 and self.slB < 4:
69                     self.scA += 1
70                     break
71                 elif self.slB >= 4 and self.slA<4:
72                     self.scB += 1
73                     break
74                 
75 def main():
76     A = eval(input())
77     B = eval(input())
78     n = int(input())
79     b = bisai(A, B, n)
80     b.bsai()
81     print("裁判员:2020310143102  董婉儿")
82     print("A赢的场次",b.scA,"B赢的场次",b.scB)
83     print("A胜率{:.2f}%".format(b.scA/b.n*100))
84     print("B胜率{:.2f}%".format(b.scB/b.n*100))
85 if __name__ == '__main__':
86     main()

 

 

 

 

 

标签:12,预测,self,乒乓球,break,比赛,dfA,slA,dfB
来源: https://www.cnblogs.com/dwexjlg/p/15516173.html