题目
德才论
输入:
输出:
题意
考生规则排序:
代码模板
结构体定义:
struct student{
char id[10];
int de,cai,sum;
int flag;
}stu[1000010];
针对两个成绩相同的学生,使用字典序进行从小到大进行排序
1, 两个学生分数不相同,那么分数高的排在前面;
2,否则,将姓名字典序小的排在前面
bool cmp(Student a,Student b){
if(a.score != b.score) return a.score > b.score;
else return strcmp(a.name,b.name) < 0;
}
strcmp注意:
1,strcmp函数是string.h头文件下面的,用来比较char型数组的字典序大小;
2,strcmp(str1,str2) 当str1的字典序小于str2返回一个负数,当str1的字典序等于str2时返回0,当str1的字典序大于str2返回一个正数。
本题代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct student{
char id[10];
int de;
int cai;
int sum;
int flag;//考生类别
}stu[100000];
bool cmp(student a,student b){
if(a.flag != b.flag) return a.flag < b.flag;
else if(a.sum != b.sum) return a.sum > b.sum;
else if(a.de != b.de) return a.de > b.de;
else return strcmp(a.id , b.id) < 0;
}
int main(){
int n,l,h;
scanf("%d%d%d", &n, &l, &h);
int m = n;//m为及格数
for(int i = 0;i < n ; i++){
scanf("%s%d%d", &stu[i].id, &stu[i].de, &stu[i].cai);
stu[i].sum = stu[i].de + stu[i].cai;
if(stu[i].de < l || stu[i].cai < l){
stu[i].flag = 5;
m--;
}
else if(stu[i].de >= h && stu[i].cai >= h) stu[i].flag = 1;
else if (stu[i].de >= h && stu[i].cai < h) stu[i].flag = 2;
else if (stu[i].de < h && stu[i].cai < h && stu[i].de >= stu[i].cai) stu[i].flag = 3;
else stu[i].flag = 4;
}
sort(stu,stu+n,cmp);
printf("%d\n",m);
for(int i = 0;i < m ; i++){
printf("%s %d %d\n",stu[i].id,stu[i].de,stu[i].cai);
}
return 0;
}
结果:
完结
最近很喜欢英文歌 : The show