# 预测比赛成绩

from random import random
def printIntro():
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 simNGames(n, probA, probB):
winsA, winsB = 0, 0
for i in range(n):
scoreA, scoreB = simOneGame(probA, probB)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
return winsA, winsB
def gameOver(a,b):
if a-b2 or b-a2:
return a or b
else:
return a11 or b11
def simOneGame(probA, probB):
scoreA, scoreB = 0, 0
serving = “A”
while not gameOver(scoreA, scoreB) and scoreA<=10 and scoreB<=10:
if serving == “A”:
if random() < probA:
scoreA += 1
else:
serving=“B”
else:
if random() < probB:
scoreB += 1
else:
serving=“A”
return scoreA, scoreB
def printSummary(winsA, winsB):
n = winsA + winsB
print(“竞技分析开始，共模拟{}场比赛”.format(n))
print(“选手A获胜{}场比赛，占比{:0.1%}”.format(winsA, winsA/n))
print(“选手B获胜{}场比赛，占比{:0.1%}”.format(winsB, winsB/n))
def main():
printIntro()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(winsA, winsB)
main()

03-21 1万+

05-15 91

02-27 640

05-15 112

10-21 177

12-21 917

12-24 247

12-06 348

03-07 24

09-04 267

12-08 321

04-03 245

06-04 15

03-27 84

09-30 127

08-20 29

10-13 161

10-14 48

10-14 153

10-14 77

11-09 30

11-09 198

11-09 265

11-10 144

11-20 85

11-21 108

12-02 276

12-07 137

12-16 272

02-29 1331

01-20 33

02-22 1577

03-03 307

03-05 205

04-18 31

04-14 66

04-19 2066

04-21 25

04-27 52

04-28 129

05-01 89

04-29 122

04-30 487

05-01 84

05-03 167

05-03 57

05-04 44

05-06 86

05-07 241

05-09 71

05-10 44

05-13 56

05-14 193

05-26 2万+

12-15 3434

04-06 1915

05-28 4566

07-09 3473

08-16 1489

10-13 696

10-17 794

11-20 991

12-13 44

12-18 8958

01-27 2682

01-25 1197

01-31 5476

02-27 5953

03-01 260

05-10 925

04-24 993

05-16 1136

05-26 344

06-14 5583

06-26 105

#### 个人总结：K近邻（KNN）

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客