#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct STU{
char id[12];
int v_sco;
int t_sco;
int total;
}student[100010],sage[100010],noble[100010],fool[100010],rest[100010];
//比较函数
bool cmp(STU a,STU b)
{
if(a.total!=b.total) return a.total>b.total;
else if(a.total==b.total&&a.v_sco!=b.v_sco) return a.v_sco>b.v_sco;
else return strcmp(a.id,b.id)<0;
}
int main(void)
{
int n,common,nice,x=0,a=0,b=0,c=0,d=0; //学生总数,及格线,优秀线,被记录人数、sage,noble,fool,rest人数
while(scanf("%d %d %d",&n,&common,&nice)!=EOF)
{
for(int i=0;i<n;i++) //录入学生信息
{
scanf("%s %d %d",student[i].id,&(student[i].v_sco),&(student[i].t_sco));
//及格
if(student[i].v_sco>=common&&student[i].t_sco>=common)
{
x++;
//sage
if(student[i].v_sco>=nice&&student[i].t_sco>=nice)
{
strcpy(sage[a].id,student[i].id);
sage[a].v_sco=student[i].v_sco;
sage[a].t_sco=student[i].t_sco;
sage[a].total=sage[a].v_sco+sage[a].t_sco;
a++;
}
//noblemen
else if(student[i].v_sco>=nice&&student[i].t_sco<nice)
{
strcpy(noble[b].id,student[i].id);
noble[b].v_sco=student[i].v_sco;
noble[b].t_sco=student[i].t_sco;
noble[b].total=noble[b].v_sco+noble[b].t_sco;
b++;
}
//foolmen
else if(student[i].v_sco>=student[i].t_sco)
{
strcpy(fool[c].id,student[i].id);
fool[c].v_sco=student[i].v_sco;
fool[c].t_sco=student[i].t_sco;
fool[c].total=fool[c].v_sco+fool[c].t_sco;
c++;
}
//rest
else
{
strcpy(rest[d].id,student[i].id);
rest[d].v_sco=student[i].v_sco;
rest[d].t_sco=student[i].t_sco;
rest[d].total=rest[d].v_sco+rest[d].t_sco;
d++;
}
}
}
printf("%d\n",x);
//排序
sort(sage,sage+a,cmp);
for(int i=0;i<a;i++)
{
printf("%s %d %d\n",sage[i].id,sage[i].v_sco,sage[i].t_sco);
}
sort(noble,noble+b,cmp);
for(int i=0;i<b;i++)
{
printf("%s %d %d\n",noble[i].id,noble[i].v_sco,noble[i].t_sco);
}
sort(fool,fool+c,cmp);
for(int i=0;i<c;i++)
{
printf("%s %d %d\n",fool[i].id,fool[i].v_sco,fool[i].t_sco);
}
sort(rest,rest+d,cmp);
for(int i=0;i<d;i++)
{
printf("%s %d %d\n",rest[i].id,rest[i].v_sco,rest[i].t_sco);
}
}
}
修改 :使用了flag变量来区分学生类别,避免代码冗长
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct STU{
char id[15];
int v_sco;
int t_sco;
int total;
int flag;
}student[100010];
bool cmp(STU a,STU b)
{
if(a.flag!=b.flag) return a.flag<b.flag;
else if(a.total!=b.total) return a.total>b.total;
else if(a.v_sco!=b.v_sco) return a.v_sco>b.v_sco;
else return strcmp(a.id,b.id)<0;
}
int main(void)
{
int m,common,nice,num; //考生人数,及格线,优秀线,通过数
while(scanf("%d %d %d",&m,&common,&nice)!=EOF)
{
int i;
num=m;
for(i=0;i<m;i++)
{
scanf("%s %d %d",student[i].id,&(student[i].v_sco),&(student[i].t_sco));
student[i].total=student[i].v_sco+student[i].t_sco;
//flag归类
if(student[i].v_sco>=common&&student[i].t_sco>=common)
{
if(student[i].v_sco>=nice&&student[i].t_sco>=nice) student[i].flag=1;
else if(student[i].v_sco>=nice&&student[i].t_sco<nice) student[i].flag=2;
else if(student[i].v_sco>=student[i].t_sco) student[i].flag=3;
else student[i].flag=4;
}
else
{
student[i].flag=5;
num--; //及格人数减1
}
}
sort(student,student+m,cmp);
//输出
printf("%d\n",num);
for(int i=0;i<num;i++)
{
printf("%s %d %d\n",student[i].id,student[i].v_sco,student[i].t_sco);
}
}
}