模拟EXCEL排序
Excel可以对一组纪录按任意指定列排序。现请编写程序实现类似功能。
输入格式:
输入的第一行包含两个正整数N(≤105) 和C,其中N是纪录的条数,C是指定排序的列号。之后有 N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,保证没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩([0, 100]内的整数)组成,相邻属性用1个空格隔开。
输出格式:
在N行中输出按要求排序后的结果,即:当C=1时,按学号递增排序;当C=2时,按姓名的非递减字典序排序;当C=3时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。
输入样例:
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
输出样例:
000001 Zoe 60
000007 James 85
000010 Amy 90
#include<bits/stdc++.h>
using namespace std;
struct node
{
string sno, name;
int score;
}p[100011];
bool cmp1(const node a, const node b)
{
return a.sno < b.sno;
}
bool cmp2(const node a, const node b)
{
if(a.name != b.name)
return a.name < b.name;
if(a.sno != b.sno)
return a.sno < b.sno;
}
bool cmp3(const node a, const node b)
{
if(a.score != b.score)
return a.score < b.score;
if(a.sno != b.sno)
return a.sno < b.sno;
}
int n, m,i;
int main()
{
cin>>n>>m;
for(i = 0; i < n; i++)
{
cin>>p[i].sno>>p[i].name>>p[i].score;
}
if(m==1)
sort(p,p+n,cmp1);
else if(m==2)
sort(p,p+n,cmp2);
else
sort(p,p+n,cmp3);
for(i = 0; i < n; i++)
{
///不能用cout输出,超时
const char *I = p[i].sno.c_str();
const char *N = p[i].name.c_str();
printf("%s %s %d\n",I,N,p[i].score);
}
//cout<<p[i].sno<<" "<<p[i].name<<" "<<p[i].score<<endl;
return 0;
}