7-37 模拟EXCEL排序 (25 分)

7-37 模拟EXCEL排序 (25 分)

Excel可以对一组纪录按任意指定列排序。现请编写程序实现类似功能。

输入格式:

输入的第一行包含两个正整数N(≤105) 和C,其中N是纪录的条数,C是指定排序的列号。之后有 N行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,保证没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩([0, 100]内的整数)组成,相邻属性用1个空格隔开。

输出格式:

N行中输出按要求排序后的结果,即:当C=1时,按学号递增排序;当C=2时,按姓名的非递减字典序排序;当C=3时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

输入样例:

1
2
3
4
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

输出样例:

1
2
3
000001 Zoe 60
000007 James 85
000010 Amy 90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>

using namespace std;

struct student{
int number;
char name[9];
int score;
};

int N, C;
vector<student> students;

bool cmp(student s1, student s2);

int main(){
scanf("%d %d", &N, &C);
for(int i = 0; i < N; i++){
int number;
char name[9];
int score;
scanf("%d %s %d", &number, name, &score);
student stu;
stu.number = number;
strcpy(stu.name, name);
stu.score = score;
students.push_back(stu);
}
sort(students.begin(), students.end(), cmp);
for(int i = 0; i < N; i++){
printf("%06d %s %d\n", students[i].number, students[i].name, students[i].score);
}
return 0;
}

bool cmp(student s1, student s2){
if(C == 1){
return s1.number < s2.number;
}else if(C == 2){
int cmpName = strcmp(s1.name, s2.name);
if(cmpName == 0){
return s1.number < s2.number;
}else{
return cmpName < 0;
}
}else{
if(s1.score == s2.score){
return s1.number < s2.number;
}else{
return s1.score < s2.score;
}
}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×