模拟体育竞技分析

import random

def print_info():
print("="50)
print("乒乓球比赛胜率模拟程序 | 作者学号后两位:XX")
print("规则:单场7局4胜,单局先11分胜;10平需净胜2分")
print("输入说明:选手1能力值、选手2能力值、模拟总场次")
print("="
50)

def play_one_set(p1_power, p2_power):
s1 = s2 = 0
while True:
total = p1_power + p2_power
point = random.random() * total
if point < p1_power:
s1 += 1
else:
s2 += 1
if (s1 >= 11 or s2 >= 11) and abs(s1 - s2) >= 2:
return 1 if s1 > s2 else 0

def play_one_match(p1, p2):
w1 = w2 = 0
while w1 < 4 and w2 < 4:
win = play_one_set(p1, p2)
if win == 1:
w1 += 1
else:
w2 += 1
return 1 if w1 == 4 else 0

def main():
print_info()
pA = int(input("请输入选手A能力值:"))
pB = int(input("请输入选手B能力值:"))
times = int(input("请输入模拟比赛总场次:"))
winA = 0
for _ in range(times):
if play_one_match(pA, pB):
winA += 1
winB = times - winA
rateA = winA / times * 100
rateB = winB / times * 100
print(f"\n模拟{times}场比赛结果:")
print(f"选手A胜{winA}场,胜率{rateA:.2f}%")
print(f"选手B胜{winB}场,胜率{rateB:.2f}%")

if name == "main":
main()

posted @ 2026-06-17 21:37  何裕双  阅读(2)  评论(0)    收藏  举报