7-37 模拟EXCEL排序 (25 分)
Excel可以对一组纪录按任意指定列排序。现请编写程序实现类似功能。
输入格式:
输入的第一行包含两个正整数N(≤10
5
) 和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
其实就是对sort排序进行改造,用结构体存三组数据。
版本一:string类 (占内存大,但是重载了许多符号)
#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
string id;
string name;
int score;
};
bool cmp1(const node &a, const node &b)
{
return a.id < b.id;
}
bool cmp2(const node &a, const node &b)
{
if(a.name == b.name) return a.id<b.id;
return a.name < b.name;
}
bool cmp3(const node &a, const node &b)
{
if(a.score == b.score) return a.id<b.id;
return a.score < b.score;
}
int main()
{
int N, C;
node stu[100010];
cin>>N>>C;
for(int i = 0;i < N;i++)
{
cin>>stu[i].id>>stu[i].name>>stu[i].score;
}
switch(C)
{
case 1: sort(stu, stu+N, cmp1); break;
case 2: sort(stu, stu+N, cmp2); break;
case 3: sort(stu, stu+N, cmp3); break;
}
for(int i = 0;i < N;i++)
{
cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].score<<endl;
}
return 0;
}
版本二:char数组
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
char id[7];
char name[9];
int score;
};
bool cmp1(const node &a, const node &b)
{
return (strcmp(a.id,b.id) < 0);
}
bool cmp2(const node &a, const node &b)
{
if(!strcmp(a.name,b.name)) return (strcmp(a.id,b.id) < 0);
return (strcmp(a.name,b.name) < 0);
}
bool cmp3(const node &a, const node &b)
{
if(a.score == b.score) return (strcmp(a.id,b.id) < 0);
return a.score < b.score;
}
int main()
{
int N, C;
node stu[100010];
cin>>N>>C;
for(int i = 0;i < N;i++)
{
cin>>stu[i].id>>stu[i].name>>stu[i].score;
}
switch(C)
{
case 1: sort(stu, stu+N, cmp1); break;
case 2: sort(stu, stu+N, cmp2); break;
case 3: sort(stu, stu+N, cmp3); break;
}
for(int i = 0;i < N;i++)
{
cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].score<<endl;
}
return 0;
}
版本三:改造优先队列(代码写的有点逗…)
#include <iostream>
#include <queue>
using namespace std;
struct node_1
{
string id;
string name;
int score;
friend bool operator < (const node_1 &a, const node_1 &b)
{
return a.id > b.id;
}
};
struct node_2
{
string id;
string name;
int score;
friend bool operator < (const node_2 &a, const node_2 &b)
{
if(a.name == b.name) return a.id>b.id;
return a.name > b.name;
}
};
struct node_3
{
string id;
string name;
int score;
friend bool operator < (const node_3 &a, const node_3 &b)
{
if(a.score == b.score) return a.id>b.id;
return a.score > b.score;
}
};
int main()
{
int N, C;
cin>>N>>C;
switch(C)
{
case 1:
{
priority_queue<node_1> Q;
for(int i = 0;i < N;i++)
{
node_1 stu;
cin>>stu.id>>stu.name>>stu.score;
Q.push(stu);
}
for(int i = 0;i < N;i++)
{
node_1 stu = Q.top();
Q.pop();
cout<<stu.id<<" "<<stu.name<<" "<<stu.score<<endl;
}
break;
}
case 2:
{
priority_queue<node_2> Q;
for(int i = 0;i < N;i++)
{
node_2 stu;
cin>>stu.id>>stu.name>>stu.score;
Q.push(stu);
}
for(int i = 0;i < N;i++)
{
node_2 stu = Q.top();
Q.pop();
cout<<stu.id<<" "<<stu.name<<" "<<stu.score<<endl;
}
break;
}
case 3:
{
priority_queue<node_3> Q;
for(int i = 0;i < N;i++)
{
node_3 stu;
cin>>stu.id>>stu.name>>stu.score;
Q.push(stu);
}
for(int i = 0;i < N;i++)
{
node_3 stu = Q.top();
Q.pop();
cout<<stu.id<<" "<<stu.name<<" "<<stu.score<<endl;
}
break;
}
}
return 0;
}