pat乙1015
1015 德才论 (25 分)
宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”
现给出一批考生的德才分数,请根据司马光的理论给出录取排名。
输入格式:
输入第一行给出 3 个正整数,分别为:N(≤10^5),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。
随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。
输出格式:
输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。
题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805307551629312
方法一:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct node{
string id;
int d,c;
};
int cmp(struct node a,struct node b){
if((a.d+a.c)!=(b.d+b.c))return (a.d+a.c)>(b.d+b.c);
else if(a.d!=b.d)return a.d>b.d;
else return a.id<b.id;
}
int main(){
int N,L,H;
node temp;
vector<node> v[4];
scanf("%d %d %d",&N,&L,&H);
int toal=N;
for(int i=0;i<N;i++){
cin>>temp.id>>temp.d>>temp.c;
if(temp.d<L||temp.c<L)toal--;
else if(temp.d>=H&&temp.c>=H)v[0].push_back(temp);
else if(temp.d>=H&&temp.c<H)v[1].push_back(temp);
else if(temp.d<H&&temp.c<H&&temp.d>=temp.c)v[2].push_back(temp);//注意此地方的条件
else v[3].push_back(temp);
}
printf("%d\n",toal);
for(int i=0;i<4;i++){
sort(v[i].begin(),v[i].end(),cmp);
for(int j=0;j<v[i].size();j++){
cout<<v[i][j].id<<" "<<v[i][j].d<<" "<<v[i][j].c<<endl;
}
}
}
方法二:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct student{
char id[10];
int de,cai,sum;
int flag;
}stu[100010];
int 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;
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>=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);
}
}
``
浙公网安备 33010602011771号