问题描述: 参加运动会的n个学校编号为1~n。比赛分成m个男子项目和w个女子项目,项目编号分别为1~m和m+1~m+w。由于各项目参加人数差别较大,有些项目取前五名,得分顺序为7,5,3,2,1;还有些项目只取前三名,得分顺序为5,3,2。哪些项目取前五名或前三名由学生自己设定。写一个统计程序产生各种成绩单和得分报表。 基本要求: (1)各项目结束时,输入前三名或前五名的项目编号、运动员姓名、校名和名次(成绩); (2)产生各学校的成绩单,内容包括每个学校所取得的每项成绩的项目号、名次(成绩)、姓名和得分,并统计各学校总分; (3)可以按学校编号、男女团体总分排序输出; (4)可以按学校编号查询学校某个项目的情况; (5)可以按项目编号查询取得前三或前五名的学校。
时间: 2024-02-03 17:13:50 浏览: 67
c语言小游戏c语言项目源码大全合集(60例).zip
5星 · 资源好评率100%
以下是一个简单的实现,包括了基本要求中的所有功能:
```python
class School:
def __init__(self, name):
self.name = name
self.scores = {}
self.total_score = 0
def add_score(self, event_id, athlete_name, rank):
if event_id not in self.scores:
self.scores[event_id] = []
self.scores[event_id].append((athlete_name, rank))
score = 0
if event_id in [1, 2, 3, 4, 5]: # 前五名得分
if rank == 1:
score = 7
elif rank == 2:
score = 5
elif rank == 3:
score = 3
elif rank == 4:
score = 2
elif rank == 5:
score = 1
else: # 前三名得分
if rank == 1:
score = 5
elif rank == 2:
score = 3
elif rank == 3:
score = 2
self.total_score += score
return score
class Event:
def __init__(self, event_id, gender, name):
self.event_id = event_id
self.gender = gender
self.name = name
self.winners = []
def add_winner(self, school_name, athlete_name, rank):
self.winners.append((school_name, athlete_name, rank))
schools = {}
events = {
1: Event(1, 'M', '男子100米赛跑'),
2: Event(2, 'M', '男子200米赛跑'),
3: Event(3, 'M', '男子400米赛跑'),
4: Event(4, 'M', '男子800米赛跑'),
5: Event(5, 'W', '女子100米赛跑'),
6: Event(6, 'W', '女子200米赛跑'),
7: Event(7, 'W', '女子400米赛跑'),
8: Event(8, 'W', '女子800米赛跑')
}
while True:
print('请选择操作:')
print('1. 输入比赛结果')
print('2. 生成成绩单')
print('3. 排序输出')
print('4. 查询学校')
print('5. 查询项目')
choice = input('请输入数字(按q退出):')
if choice == 'q':
break
if choice == '1':
event_id = int(input('请输入项目编号:'))
school_name = input('请输入学校名:')
athlete_name = input('请输入运动员名:')
rank = int(input('请输入名次(成绩):'))
if school_name not in schools:
schools[school_name] = School(school_name)
score = schools[school_name].add_score(event_id, athlete_name, rank)
events[event_id].add_winner(school_name, athlete_name, rank)
print('得分:%d' % score)
elif choice == '2':
for school_name, school in schools.items():
print(school_name)
for event_id, scores in school.scores.items():
event = events[event_id]
print('%d %s' % (event_id, event.name))
for athlete_name, rank in scores:
print('\t%s %d' % (athlete_name, rank))
print('总分:%d' % school.total_score)
print()
elif choice == '3':
sort_by = input('请选择排序方式(1. 学校编号 2. 男子团体总分 3. 女子团体总分):')
if sort_by == '1':
sorted_schools = sorted(schools.items(), key=lambda x: x[0])
elif sort_by == '2':
sorted_schools = sorted(schools.items(), key=lambda x: sum([school.add_score(event_id, '', rank) for event_id, scores in x[1].scores.items() for _, rank in scores if events[event_id].gender == 'M']))
elif sort_by == '3':
sorted_schools = sorted(schools.items(), key=lambda x: sum([school.add_score(event_id, '', rank) for event_id, scores in x[1].scores.items() for _, rank in scores if events[event_id].gender == 'W']))
for school_name, school in sorted_schools:
print(school_name, school.total_score)
print()
elif choice == '4':
school_name = input('请输入学校名:')
if school_name not in schools:
print('学校不存在')
else:
for event_id, scores in schools[school_name].scores.items():
event = events[event_id]
print('%d %s' % (event_id, event.name))
for athlete_name, rank in scores:
print('\t%s %d' % (athlete_name, rank))
print('总分:%d' % schools[school_name].total_score)
print()
elif choice == '5':
event_id = int(input('请输入项目编号:'))
event = events[event_id]
print('%s(%s)' % (event.name, '男子' if event.gender == 'M' else '女子'))
for school_name, athlete_name, rank in event.winners:
print('%s %s %d' % (school_name, athlete_name, rank))
print()
```
这个程序可以不断接收用户输入,根据用户的选择进行不同的操作。用户可以输入比赛结果,生成成绩单,按学校编号、男女团体总分排序输出,查询学校某个项目的情况,以及查询取得前三或前五名的学校。每个学校和每个项目都对应一个类,可以方便地进行管理和统计。
阅读全文