模拟体育竞技分析

发布时间 2023-12-26 23:49:21作者: gzygshyq

模拟体育竞技分析
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬a.采用乒乓球比赛规则(学号尾号为0,1,2,3同学必做及格题)。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪(即:一局比赛:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬在一局比赛中,先得11分的一方为胜方;10平后,先多得2分的一方为胜方。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬一场比赛:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬单打的淘汰赛采用七局四胜制,双打淘汰赛和团体赛采用五局三胜制。)
博客基本要求:‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
参‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬考书上223-224。或者https://blog.csdn.net/sxingming/article/details/51235650‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
h‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬ttps://blog.csdn.net/qq_40855366/article/details/81177203‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
https://blog.csdn.net/qq_40390825/article/details/82467008‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
1.打印程序的介绍性信息式(要求包括你们学号后两位信息)
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬2.输入I(input):两个球员(球队)的能力值,模拟比赛的次数‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬3.处理P(process):模拟比赛过程‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬4.输出O(output):两个球员(球队)获胜的概率(输出谁做的结果截图)‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪5. 运用pyinstaller打包可以执行的文件

单打淘汰赛的代码如下:

 1 ‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬#单打的淘汰赛采用七局四胜制
 2 print("学号后两位为03(2021310143103)")
 3 
 4 from random import random
 5 def printIntro():          #打印程序介绍信息
 6     print("这个程序模拟两个选手A和B的乒乓球竞技比赛")
 7     print("程序运行需要A和B的能力值(以0到1之间的小数表示)")
 8 def getInputs():           #获得程序运行参数
 9     a = eval(input("请输入选手A的能力值(0-1): "))
10     b = eval(input("请输入选手B的能力值(0-1): "))
11     n = eval(input("模拟比赛的场次: "))
12     return a, b, n
13 def simNGames(n, probA, probB):    # 进行N场比赛
14     winsA, winsB = 0, 0
15     for i in range(n):
16         for j in range(7):           #进行7局4胜的比赛
17             scoreA, scoreB = simOneGame(probA, probB)
18             if scoreA > scoreB:
19                 winsA += 1
20             else:
21                 winsB += 1
22     return winsA, winsB
23 def gameOver(a,b):               #正常比赛结束
24     return a==11 or b==11
25 def gameOver2(a,b):              #进行抢12比赛结束
26    if abs((a-b))>=2:
27        return a,b
28 def simOneGame(probA, probB):         #进行一场比赛
29     scoreA, scoreB = 0, 0           #初始化AB的得分
30     serving = "A"                 
31     while not gameOver(scoreA, scoreB):     #用while循环来执行比赛
32         if scoreA==10 and scoreB==10:
33             return(simtwoGame2(probA,probB))
34         if serving == "A":
35             if random() < probA:            ##用随机数生成胜负
36                 scoreA += 1
37             else:
38                 serving="B"
39         else:
40             if random() < probB:
41                 scoreB += 1
42             else:
43                 serving="A"
44     return scoreA, scoreB
45 def simtwoGame2(probA,probB):
46     scoreA,scoreB=10,10
47     serving = "A"
48     while not gameOver2(scoreA, scoreB):
49         if serving == "A":
50             if random() < probA:
51                 scoreA += 1
52             else:
53                 serving="B"
54         else:
55             if random() < probB:
56                 scoreB += 1
57             else:
58                 serving="A"
59     return scoreA, scoreB
60 def printSummary(winsA, winsB):
61     n = winsA + winsB
62     print("竞技分析开始,共模拟{}场比赛".format(n))
63     print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
64     print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))
65 def main():
66     printIntro()
67     probA, probB, n = getInputs()
68     winsA, winsB = simNGames(n, probA, probB)
69     printSummary(winsA, winsB)
70 main()

运行结果:

 

双打淘汰赛和团体赛的代码如下:

 1 #双打淘汰赛和团体赛采用五局三胜制
 2 print("学号后两位为03(2021310143103)")
 3 
 4 from random import random 
 5 def printIntro():          #打印程序介绍信息
 6     print("这个程序模拟两个队伍A和B的乒乓球竞技比赛")
 7     print("程序运行需要队伍A和队伍B的能力值(以0到1之间的小数表示)")
 8 def getInputs():           #获得程序运行参数
 9     a = eval(input("请输入队伍A的能力值(0-1): "))
10     b = eval(input("请输入队伍B的能力值(0-1): "))
11     n = eval(input("模拟比赛的场次: "))
12     return a, b, n
13 def simNGames(n, probA, probB):    # 进行N场比赛
14     winsA, winsB = 0, 0
15     for i in range(n):
16         for j in range(5):           #进行5局3胜的比赛
17             scoreA, scoreB = simOneGame(probA, probB)
18             if scoreA > scoreB:
19                 winsA += 1
20             else:
21                 winsB += 1
22     return winsA, winsB
23 def gameOver(a,b):               #正常比赛结束
24     return a==11 or b==11
25 def gameOver2(a,b):              #进行抢12比赛结束
26     return a==12 or b==12
27 def simOneGame(probA, probB):         #进行一场比赛
28     scoreA, scoreB = 0, 0           #初始化AB的得分
29     serving = "A"                 
30     while not gameOver(scoreA, scoreB):     #用while循环来执行比赛
31         if scoreA==10 and scoreB==10:
32             return(simtwoGame2(probA,probB))
33         if serving == "A":
34             if random() < probA:            ##用随机数生成胜负
35                 scoreA += 1
36             else:
37                 serving="B"
38         else:
39             if random() < probB:
40                 scoreB += 1
41             else:
42                 serving="A"
43     return scoreA, scoreB
44 def simtwoGame2(probA,probB):
45     scoreA,scoreB=10,10
46     serving = "A"                 #假如先让队伍A发球
47     while not gameOver2(scoreA, scoreB):
48         if serving == "A":
49             if random() < probA:
50                 scoreA += 1
51             else:
52                 serving="B"
53         else:
54             if random() < probB:
55                 scoreB += 1
56             else:
57                 serving="A"
58     return scoreA, scoreB
59 def printSummary(winsA, winsB):
60     n = winsA + winsB
61     print("竞技分析开始,共模拟{}场比赛".format(n))
62     print("队伍A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
63     print("队伍B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))
64 def main():
65     printIntro()
66     probA, probB, n = getInputs()
67     winsA, winsB = simNGames(n, probA, probB)
68     printSummary(winsA, winsB)
69 main()

运行结果: