开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第18天,点击查看活动详情
[NOIP2007 普及组] 奖学金
题目描述
某小学最近得到了一笔赞助,打算拿出其中一部分为学习成绩优秀的前 名学生发奖学金。期末,每个学生都有 门课的成绩:语文、数学、英语。先按总分从高到低排序,如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学 排在前面,这样,每个学生的排序是唯一确定的。
任务:先根据输入的 门课的成绩计算总分,然后按上述规则排序,最后按排名顺序输出前五名名学生的学号和总分。注意,在前 名同学中,每个人的奖学金都不相同,因此,你必须严格按上述规则排序。例如,在某个正确答案中,如果前两行的输出数据(每行输出两个数:学号、总分) 是:
这两行数据的含义是:总分最高的两个同学的学号依次是 号、 号。这两名同学的总分都是 (总分等于输入的语文、数学、英语三科成绩之和) ,但学号为 的学生语文成绩更高一些。如果你的前两名的输出数据是:
则按输出错误处理,不能得分。
输入格式
共 行。
第 行为一个正整数,表示该校参加评选的学生人数。
第 到 行,每行有 个用空格隔开的数字,每个数字都在 到 之间。第 行的 个数字依次表示学号为 的学生的语文、数学、英语的成绩。每个学生的学号按照输入顺序编号为 (恰好是输入数据的行号减 )。
所给的数据都是正确的,不必检验。
//感谢 黄小U饮品 修正输入格式
输出格式
共 行,每行是两个用空格隔开的正整数,依次表示前 名学生的学号和总分。
样例 #1
样例输入 #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
样例输入 #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
分析
这是一个语法题,考察了结构体的用法以及cmp排序,按照题目描述模拟先比总分,再比语文成绩,最后输出就ok啦!
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <stack>
#include <cmath>
#include <iomanip>
#define ll long long
#define AC return
#define Please 0
using namespace std;
const int N=310;
const double esp=1e-8;
typedef pair<int,int>PII;
typedef unsigned long long ull;
unordered_map<string, pair<char, string>> pre;
unordered_map<string,int>mp;
inline int read(){//快读
int x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9'){
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0' && ch<='9'){
x=x*10+ch-'0';
ch=getchar();
}
AC x*f;
}
struct mes{
int id,ch,ma,en,tot;
}a[N];
int n;
bool cmp(mes x,mes y){
if(x.tot==y.tot){
if(x.ch==y.ch){
return x.id<y.id;
}
return x.ch>y.ch;
}
return x.tot>y.tot;
}
int main(){
n=read();
for(int i=1;i<=n;i++){
a[i].id=i;
cin>>a[i].ch>>a[i].ma>>a[i].en;
a[i].tot=a[i].ch+a[i].en+a[i].ma;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=5;i++){
cout<<a[i].id<<" "<<a[i].tot<<endl;
}
return 0;
}