7-5 模拟乒乓球比赛
题目描述
中国选手与日本选手进行乒乓球比赛,每局先得11分者赢得此局,整个比赛5局3胜,如某队获胜达到3局,则赢得此场比赛。
假设中国选手每一分获胜的概率为m,(含m,m<1)。
请模拟比赛过程,并输出各队获胜情况。
请注意:
请使用random库中的random模拟胜率
不考虑10平要多得2分的情况
输入格式
请在第一行输入整数随机种子。
请在第二行输入中国队每一分获胜的概率(大于0小于1的数)
请在第三行输入要模拟的比赛次数
输出格式
对每一组输入,根据模拟结果在一行中输出:
Chinese team:m,Japanese team:n。
输入样例
在这里给出一组输入。例如:
3
0.6
100
输出样例
Chinese team:98,Japanese team:2
代码
import random
theSeed = int(input())
pro = eval(input())
n = int(input())
random.seed(theSeed)
score1, score2 = 0, 0 #局内比分
A, B = 0, 0 #五局三胜比分统计
res1, res2 = 0, 0 #各自赢得比赛的场数
for i in range(n):
for i in range(5):
for i in range(21):
if random.random() <= pro:
score1 = score1 + 1
else:
score2 = score2 + 1
if score1 == 11:
A = A + 1
score1 = 0
score2 = 0
break
if score2 == 11:
B = B + 1
score1 = 0
score2 = 0
break;
if A == 3:
res1 = res1 + 1
A = 0
B = 0
break
if B == 3:
res2 = res2 + 1
A = 0
B = 0
break
print("Chinese team:{},Japanese team:{}"
.format(res1, res2))