一、排球比赛模拟程序
赛制规定:前4局比赛采用25分制,每个队只有赢得至少25分,并同时超过对方2分时 ,才胜1局。正式比赛采用5局3胜制,决赛局的比赛采用15分支,一队先得8分后,两队交换场区,按原位置顺序继续比赛到结束。在决胜局(第五局)之比赛,先获15分并领先对方2分为胜。
代码如下:
import random
def simulate_game(team_a_skill, team_b_skill, max_score, must_win_by):
score_a, score_b = 0, 0
while True:
if random.random() < team_a_skill / (team_a_skill + team_b_skill):
score_a += 1
else:
score_b += 1
if score_a >= max_score and (score_a - score_b) >= must_win_by:
return 'A'
elif score_b >= max_score and (score_b - score_a) >= must_win_by:
return 'B'
def simulate_match(team_a_skill, team_b_skill, num_games):
wins_a, wins_b = 0, 0
for i in range(num_games):
if i < 4: # first 4 games to 25
winner = simulate_game(team_a_skill, team_b_skill, 25, 2)
else: # last game to 15
winner = simulate_game(team_a_skill, team_b_skill, 15, 2)
if winner == 'A':
wins_a += 1
else:
wins_b += 1
if wins_a == 3 or wins_b == 3:
break
return wins_a, wins_b
def main():
print("排球比赛模拟程序——学号06")
print("赛制规定:前4局比赛采用25分制,每个队只有赢得至少25分,并同时超过对方2分时 ,才胜1局。正式比赛采用5局3胜制,决赛局的比赛采用15分支,一队先得8分后,两队交换场区,按原位置顺序继续比赛到结束。在决胜局(第五局)之比赛,先获15分并领先对方2分为胜。")
team_a_skill = float(input("输入选手A的水平 (0-1): "))
team_b_skill = float(input("输入选手B的水平 (0-1): "))
num_simulations = int(input("输入模拟比赛场数: "))
wins_a, wins_b = 0, 0
for _ in range(num_simulations):
result_a, result_b = simulate_match(team_a_skill, team_b_skill, 5)
wins_a += result_a
wins_b += result_b
print(f"After {num_simulations} simulations:")
print(f"选手A获胜的概率为 {wins_a / (wins_a + wins_b):.2%} ")
print(f"选手B获胜的概率为 {wins_b / (wins_a + wins_b):.2%} ")
if __name__ == "__main__":
main()
运行结果如下:
二、足球比赛模拟程序
赛制规定:获胜一方得三分,双方打平各得一分,输球自然零分,最后累计分高的队伍获胜。
代码如下:
import random
def print_introduction():
print("足球比赛模拟程序——学号06")
print("赛制规定:获胜一方得三分,双方打平各得一分,输球自然零分,最后累计分高的队伍获胜。")
def input_players():
player1 = float(input("请输入球员A的能力值(0-1):"))
player2 = float(input("请输入球员B的能力值(0-1):"))
return player1, player2
def simulate_game(player1, player2):
score1 = 0
score2 = 0
for _ in range(3):
if random.random() < player1 / (player1 + player2):
score1 += 1
elif random.random() < player2 / (player1 + player2):
score2 += 1
return score1, score2
def calculate_probability(score1, score2):
total_games = score1 + score2
probability1 = score1 / total_games
probability2 = score2 / total_games
return probability1, probability2
def main():
print_introduction()
player1, player2 = input_players()
total_games = int(input("请输入模拟比赛的次数:"))
score1 = 0
score2 = 0
for _ in range(total_games):
s1, s2 = simulate_game(player1, player2)
score1 += s1
score2 += s2
probability1, probability2 = calculate_probability(score1, score2)
print(f"球员A获胜的概率为:{probability1:.2%}")
print(f"球员B获胜的概率为:{probability2:.2%}")
if __name__ == "__main__":
main()
运行结果如下:
三、采用面向对象编程模拟排球比赛
赛制规定:前4局比赛采用25分制,每个队只有赢得至少25分,并同时超过对方2分时 ,才胜一局。正式比赛采用5局3胜制,决胜局的比赛采用15分制,一队先得8分后,两队交换场区,按原位置顺序继续比赛到结束。在决胜局(第五局)之比赛,先获15分并领先对方2分为胜。同时采用循环赛或者晋级赛形式,模拟分析4个队。
代码如下:
import random
class Team:
def __init__(self, skill,name):
self.skill = skill
self.name = name
self.score = 0
self.sets_won = 0
def score_point(self):
self.score += 1
def win_set(self):
self.sets_won += 1
self.score = 0 # 重置分数
def reset_score(self):
self.score = 0
class VolleyballMatch:
def __init__(self, team_a, team_b):
self.team_a = team_a
self.team_b = team_b
def play_set(self, max_score, must_win_by):
while True:
if random.random() < self.team_a.skill / (self.team_a.skill + self.team_b.skill):
self.team_a.score_point()
else:
self.team_b.score_point()
if self.team_a.score >= max_score and (self.team_a.score - self.team_b.score) >= must_win_by:
self.team_a.win_set()
break
elif self.team_b.score >= max_score and (self.team_b.score - self.team_a.score) >= must_win_by:
self.team_b.win_set()
break
self.team_a.reset_score()
self.team_b.reset_score()
def play_match(self):
while self.team_a.sets_won < 3 and self.team_b.sets_won < 3:
if self.team_a.sets_won + self.team_b.sets_won < 4: # 前4局
self.play_set(25, 2)
else: # 决胜局
self.play_set(15, 2)
return self.team_a if self.team_a.sets_won > self.team_b.sets_won else self.team_b
def simulate_tournament(teams, num_simulations):
wins = {team.name: 0 for team in teams}
for _ in range(num_simulations):
random.shuffle(teams) # 打乱队伍顺序
for i in range(len(teams)):
for j in range(i + 1, len(teams)):
match = VolleyballMatch(teams[i], teams[j])
winner = match.play_match()
wins[winner.name] += 1
# 输出排名
ranked_teams = sorted(wins.items(), key=lambda item: item[1], reverse=True)
for rank, (team_name, win_count) in enumerate(ranked_teams, start=1):
print(f"排名 {rank}: {team_name} 赢 {win_count} 次")
return ranked_teams
def main():
print("采用面向对象编程模拟排球比赛—学号06")
print("赛制规定:前4局比赛采用25分制,每个队只有赢得至少25分,并同时超过对方2分时 ,才胜一局。正式比赛采用5局3胜制,决胜局的比赛采用15分制,一队先得8分后,两队交换场区,按原位置顺序继续比赛到结束。在决胜局(第五局)之比赛,先获15分并领先对方2分为胜。同时采用循环赛或者晋级赛形式,模拟分析4个队。")
team_a = Team(skill=0.6, name="Team A")
team_b = Team(skill=0.5, name="Team B")
team_c = Team(skill=0.55, name="Team C")
team_d = Team(skill=0.45, name="Team D")
teams = [team_a, team_b, team_c, team_d]
num_simulations = int(input("输入模拟比赛的场数: "))
simulate_tournament(teams, num_simulations)
if __name__ == "__main__":
main()
运行结果如下: