C++ 编程题:小学奖学金发放排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
int id;
int chinese;
int math;
int english;
int total;
};
bool compare(const Student& s1, const Student& s2) {
if (s1.total != s2.total) {
return s1.total > s2.total;
} else if (s1.chinese != s2.chinese) {
return s1.chinese > s2.chinese;
} else {
return s1.id < s2.id;
}
}
int main() {
int n;
cin >> n;
vector<Student> students(n);
for (int i = 0; i < n; i++) {
cin >> students[i].chinese >> students[i].math >> students[i].english;
students[i].id = i + 1;
students[i].total = students[i].chinese + students[i].math + students[i].english;
}
sort(students.begin(), students.end(), compare);
for (int i = 0; i < 5; i++) {
cout << students[i].id << ' ' << students[i].total << endl;
}
return 0;
}
题目描述
某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前 5 名学生发奖学金。期末,每个学生都有 3 门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学排在前面,这样,每个学生的排序是唯一确定的。
**任务:**先根据输入的 3 门课的成绩计算总分,然后按上述规则排序,最后按排名顺序输出前 5 名学生的学号和总分。注意,在前 5 名同学中,每个人的奖学金都不相同,因此,你必须严格按上述规则排序。
**例如,**在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分)是:
7 279 5 279
这两行数据的含义是:总分最高的两个同学的学号依次是 7 号、5 号。这两名同学的总分都是 279(总分等于输入的语文、数学、英语三科成绩之和),但学号为 7 的学生语文成绩更高一些。如果你的前两名的输出数据是:
5 279 7 279
则按输出错误处理,不能得分。
输入格式
包含 n+1 行:
第1行为一个正整数 n,表示该校参加评选的学生人数。
第 2 到 n+1 行,每行有3个用空格隔开的数字,每个数字都在 0 到 100 之间。第j行的3个数字依次表示学号为 j-1 的学生的语文、数学、英语的成绩。每个学生的学号按照输入顺序编号为 1…n (恰好是输入数据的行号减 1 )。
所给的数据都是正确的,不必检验。
输出格式
共有 5 行,每行是两个用空格隔开的正整数, 依次表示前 5 名学生的学号和总分。
输入输出样例
样例 1
输入样例
6
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98
输出样例
6 265
4 264
3 258
2 244
1 237
样例 2
输入样例
8
80 89 89
88 98 78
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98
输出样例
8 265
2 264
6 264
1 258
5 258
用C++
用C++做
原文地址: https://www.cveoy.top/t/topic/qoZC 著作权归作者所有。请勿转载和采集!