题目描述
某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金。期末,每个学生都有3门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学 排在前面,这样,每个学生的排序是唯一确定的。
任务:先根据输入的3门课的成绩计算总分,然后按上述规则排序,最后按排名顺序输出前五名名学生的学号和总分。注意,在前5名同学中,每个人的奖学金都不相同,因此,你必须严格按上述规则排序。例如,在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分) 是:
77 279279
55 279279
这两行数据的含义是:总分最高的两个同学的学号依次是77号、55号。这两名同学的总分都是 279279 (总分等于输入的语文、数学、英语三科成绩之和) ,但学号为77的学生语文成绩更高一些。如果你的前两名的输出数据是:
55 279279
77 279279
则按输出错误处理,不能得分。
输入格式
共n+1行。
第11行为一个正整数n( \le 300)n(≤300),表示该校参加评选的学生人数。
第22到n+1n+1行,每行有33个用空格隔开的数字,每个数字都在00到100100之间。第jj行的33个数字依次表示学号为j-1j−1的学生的语文、数学、英语的成绩。每个学生的学号按照输入顺序编号为1~n1 n(恰好是输入数据的行号减11)。
所给的数据都是正确的,不必检验。
//感谢 黄小U饮品 修正输入格式
输出格式
共5行,每行是两个用空格隔开的正整数,依次表示前55名学生的学号和总分。
输入输出样例
输入 #1 复制
6
90 67 80
87 66 91
78 89 91
88 99 77
67 89 64
78 89 98
输出 #1 复制
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
输出 #2 复制
8 265
2 264
6 264
1 258
5 258
方法一:冒泡排序
#include <iostream>
using namespace std;
class Subject {
public:
int id;
int chinese;
int math;
int english;
int total;
};
int main() {
Subject a[300],temp;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
a[i].id = i + 1;
cin >> a[i].chinese >> a[i].math >> a[i].english;
a[i].total = a[i].chinese + a[i].math + a[i].english;
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j].total < a[j + 1].total) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
else if (a[j].total == a[j + 1].total) {
if (a[j].chinese < a[j + 1].chinese) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
else if (a[j].chinese == a[j].chinese) {
if (a[j].id > a[j + 1].id) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
}
for (int i = 0; i < 5; i++) {
cout << a[i].id << " " << a[i].total << endl;
}
return 0;
}
方法二:快排(利用sort函数)
#include <iostream>
#include <algorithm>
using namespace std;
class Subject {
public:
int id;
int chinese;
int math;
int english;
int total;
}a[300], temp;
bool cmp(Subject t1, Subject t2) {
if (t1.total > t2.total)
return 1;
else if (t1.total < t2.total)
return 0;
else {
if (t1.chinese > t2.chinese)
return 1;
else if (t1.chinese < t2.chinese)
return 0;
else {
if (t1.id < t2.id)
return 1;
else
return 0;
}
}
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
a[i].id = i + 1;
cin >> a[i].chinese >> a[i].math >> a[i].english;
a[i].total = a[i].chinese + a[i].math + a[i].english;
}
sort(a , a + n, cmp);
for (int i = 0; i < 5; i++) {
cout << a[i].id << " " << a[i].total << endl;
}
return 0;
}
方法三:手写快排