天天看點

PAT甲1028 List Sorting (25)(25 分)

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct student
{
  char id[10];
  char name[10];
  int grade;
}stu[110000];

bool cmp1(student a,student b)
{
    return strcmp(a.id,b.id)<0;
}

bool cmp2(student a ,student b)
{
    if(strcmp(a.name,b.name)!=0)return strcmp(a.name,b.name)<0;
    else return strcmp(a.id,b.id)<0;
}

bool cmp3(student a,student b)
{
    if(a.grade!=b.grade)return a.grade<b.grade;
    else return strcmp(a.id,b.id)<0;
}

int N,K;

int main()
{
    scanf("%d%d",&N,&K);
    for(int i=0;i<N;i++)
    {
        scanf("%s%s%d",&stu[i].id,&stu[i].name,&stu[i].grade);
    }
    if(K==1)
    {
        sort(stu,stu+N,cmp1);
    }
    else if(K==2)
    {
        sort(stu,stu+N,cmp2);
    }
    else if(K==3)
    {
        sort(stu,stu+N,cmp3);
    }
    for(int i=0;i<N;i++)
    {
        printf("%s %s %d\n",stu[i].id,stu[i].name,stu[i].grade);
    }
    return 0;
}