编写一个班级成绩排序程序。班级有班级号,班级人数和若干学生,每个学生有学号、姓名、三门课成绩。按学生总成绩从高到底进行排序。 【输入形式】输入班级号,及班级人数;然后输入班级中每个学生信息。 【输出形式】输出班级信息,以及按总成绩从高到底输出学生信息。 【样例输入】1(班级号) 3(班级人数) 1 a 98 89 78(输入三个学生信息,学号、姓名、三门课成绩) 2 b 78 86 67 3 c 87 98 96 【样例输出】class no:1 numbers:3 3 c 87 98 96 281 1 a 98 89 78 265 2 b 78 86 67 231c
时间: 2023-12-06 17:42:12 浏览: 73
接收从键盘输入某班学生的学号、姓名、成绩,对学 生的成绩进行排序.zip
5星 · 资源好评率100%
以下是Python3代码实现:
```python
class Student:
def __init__(self, id, name, score1, score2, score3):
self.id = id
self.name = name
self.score1 = score1
self.score2 = score2
self.score3 = score3
self.total_score = score1 + score2 + score3 # 计算总成绩
def __lt__(self, other):
return self.total_score > other.total_score # 重载小于运算符,按总成绩从高到底排序
class Class:
def __init__(self, class_no, num_students):
self.class_no = class_no
self.num_students = num_students
self.students = []
def input_students(self):
for i in range(self.num_students):
id, name, score1, score2, score3 = input().split()
student = Student(int(id), name, int(score1), int(score2), int(score3))
self.students.append(student)
def sort_students(self):
self.students.sort()
def output_class_info(self):
print("class no:{} numbers:{}".format(self.class_no, self.num_students))
def output_students_info(self):
for student in self.students:
print("{} {} {} {} {} {}".format(student.id, student.name, student.score1, student.score2, student.score3, student.total_score))
class_no, num_students = input().split()
class_no = int(class_no)
num_students = int(num_students)
class_obj = Class(class_no, num_students)
class_obj.input_students()
class_obj.sort_students()
class_obj.output_class_info()
class_obj.output_students_info()
```
输入样例:
```
1 3
1 a 98 89 78
2 b 78 86 67
3 c 87 98 96
```
输出样例:
```
class no:1 numbers:3
3 c 87 98 96 281
1 a 98 89 78 265
2 b 78 86 67 231
```
阅读全文