Excel can sort records according to any column. Now you are supposed to imitate this function.
翻译:Excel可以根据任何列对记录排序。现在你需要模仿这个机制。
Each input file contains one test case. For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).
翻译:一个输入文件包含一组测试数据。对于每组输入数据,第一行包括两个正整数N (<=100000) 和C,N代表记录数,C代表你需要根据第几行排序。接下来的N行,每行包括一个学生记录。一个学生记录包括他的唯一ID(一个六位数字),名字(一个不超过8位字符没有空格的字符串),和成绩(一个在0-100之间的整数)
For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.
翻译:对于每组输入数据,输出N行排序结果。如果C=1,根据ID升序排序;如果C=2,根据姓名降序排序;如果C=3,记录必须根据成绩降序排序。如果有学生姓名相同或成绩相同,那么他们必须根据ID升序排序。
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
000001 Zoe 60
000007 James 85
000010 Amy 90
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90
定义一个结构体保存数据,重写三个排序函数,根据C来调用即可。读取数据需要用scanf,否则会超时。
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 99999999
using namespace std;
struct Stu{
int ID;
string name;
int grade;
Stu(int id,string s,int g):ID(id),name(s),grade(g){}
};
bool cmp1(const Stu &a,const Stu &b){
return a.IDbool cmp2(const Stu &a,const Stu &b){
return a.name==b.name?a.IDbool cmp3(const Stu &a,const Stu &b){
return a.grade==b.grade?a.IDvectorstu;
int N,C;
int main(){
scanf("%d%d",&N,&C);
string s;
int id,grade;
for(int i=0;iscanf("%d",&id);
cin>>s;
scanf("%d",&grade);
stu.push_back(Stu(id,s,grade));
}
switch(C){
case 1:sort(stu.begin(),stu.end(),cmp1);break;
case 2:sort(stu.begin(),stu.end(),cmp2);break;
case 3:sort(stu.begin(),stu.end(),cmp3);break;
}
for(int i=0;iprintf("%06d %s %d\n",stu[i].ID,stu[i].name.c_str(),stu[i].grade);
}
return 0;
}