做这道题的时候把我给搞晕了,既要排分数,又要排学号。查看题解后学到了用头文件algorithm中的sort函数,再自定义compar来排分数的同时排学号。
#include<cstdio>
#include<math.h>
#include<algorithm>
using namespace std;
struct student
{
int s;
int num;
}stu[5001];
int compar(student a,student b)
{
if(a.s>b.s) return true;
else if(a.s==b.s)
{
if(a.num<b.num) return true;
else return false;
}
else return false;
}
int main()
{
struct student a;
int n,m;
scanf("%d %d",&n,&m);
m=floor(m*1.5);
for(int i=0;i<n;i++) scanf("%d %d",&stu[i].num,&stu[i].s);
sort(stu,stu+n,compar);
int fenshuxian=stu[m-1].s;
int renshu=m;
for(int i=m;i<n;i++)
{
if(stu[i].s==fenshuxian) renshu++;
else if(stu[i].s<fenshuxian) break;
}
printf("%d %d\n",fenshuxian,renshu);
for(int i=0;i<renshu;i++) printf("%d %d\n",stu[i].num,stu[i].s);
return 0;
}