奖学金


某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前5名学生发奖学金。

期末,每个学生都有3门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学 排在前面,这样,每个学生的排序是唯一确定的。

输入

共 n + 1 行。

第 1 行为一个正整数 n(6 ≤ n ≤ 300),表示该校参加评选的学生人数。

第 2 到 n + 1 行,每行有 3 个用空格隔开的数字,每个数字都在 0 到 100 之间。第 j 行的 3 个数字依次表示学号为 j−1 的学生的语文、数学、英语的成绩。每个学生的学号按照输入顺序编号为 n − 1(恰好是输入数据的行号减 1)。

输出

共 5 行,每行是两个用空格隔开的正整数,依次表示前 5 名学生的学号和总分。

样例

1
2
3
4
5
6
7
8
9
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
1
2
3
4
5
8 265
2 264
6 264
1 258
5 258

思路:

  1. 构造student自定义类型,包含学生学号num、语文成绩chinese、数学成绩math、英语成绩english、总分all
  2. 进行学生信息的录入,并计算学生的总成绩
  3. 利用sort函数对student自定义类型按照给定的排序方式进行排序后进行输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<algorithm>
using namespace std;

//1.构造student自定义类型,包含学生所有信息
struct student{
int num, chinese, math, english, all;
};

student stu[305];

bool cmp(const student &a, const student &b){
if(a.all == b.all){
if(a.chinese == b.chinese){
return a.num < b.num;
}
return a.chinese > b.chinese;
}
return a.all > b.all;
}

int main(){
int n;
cin >> n;
//2.进行学生信息的录入,并计算学生的总成绩
for(int i = 0; i < n; ++i){
cin >> stu[i].chinese >> stu[i].math >> stu[i].english;
stu[i].all = stu[i].chinese + stu[i].math + stu[i].english;
stu[i].num = i + 1;
}
//3.利用sort函数对student自定义类型按照给定的排序方式进行排序后进行输出
sort(stu, stu + n, cmp);
for(int i = 0; i < 5; ++i){
cout << stu[i].num << " " << stu[i].all << endl;
}
return 0;
}