AIchatOS闪击成果:
2023-08-15 11:35:44
发布于:广东
3阅读
0回复
0点赞
#include <iostream>
#include <algorithm>
using namespace std;
struct Student {
int id; // 学号
int chinese; // 语文成绩
int math; // 数学成绩
int english; // 英语成绩
int totalScore; // 总分
};
bool compare(const Student& s1, const Student& s2) {
if (s1.totalScore != s2.totalScore) {
return s1.totalScore > s2.totalScore;
} else if (s1.chinese != s2.chinese) {
return s1.chinese > s2.chinese;
} else {
return s1.id < s2.id;
}
}
int main() {
int n;
cin >> n; // 学生人数
// 读取学生成绩
Student students[n];
for (int i = 0; i < n; i++) {
students[i].id = i + 1;
cin >> students[i].chinese >> students[i].math >> students[i].english;
students[i].totalScore = students[i].chinese + students[i].math + students[i].english;
}
// 排序并输出前五名学生的学号和总分
sort(students, students + n, compare);
for (int i = 0; i < 5; i++) {
cout << students[i].id << " " << students[i].totalScore << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个