//
// main.cpp
// PAT_1028. List Sorting
//
// Created by wjq on 17/4/14.
// Copyright © 2017年 wjq. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
struct record
{
int id;
char name[15];
int grade;
}stu[100005];
int cmpid(record a,record b)
{
return a.id<b.id;
}
int cmpname(record a,record b)
{
if(strcmp(a.name,b.name)==0)
return a.id<b.id;
return (strcmp(a.name,b.name)<0);
}
int cmpgrade(record a,record b)
{
if(a.grade==b.grade)
return a.id<b.id;
return a.grade<b.grade;
}
int N,C;
int main(int argc, const char * argv[])
{
scanf("%d%d",&N,&C);
for(int i=0;i<N;i++)
scanf("%d%s%d",&stu[i].id,stu[i].name,&stu[i].grade);
switch (C)
{
case 1:
sort(stu,stu+N,cmpid);
break;
case 2:
sort(stu,stu+N,cmpname);
break;
case 3:
sort(stu,stu+N,cmpgrade);
break;
default:
break;
}
for(int i=0;i<N;i++)
printf("%06d %s %d\n",stu[i].id,stu[i].name,stu[i].grade);
return 0;
}
排序,简单题.
习惯了用cin,但cin效率没有scanf高,本题用cin最后一个case超时,因此要用scanf