分数值:25分
通过率:35%
1. 问题描述
Excel can sort records according to any column. Now you are supposed to imitate this function.
Excel 可以根据任意列将记录排序。现在要求你模仿出这个功能。
Input Specification:
Each input file contains one test case. For each case, the first line contains two integers
N
(
≤
1
0
5
)
N (≤10^5)
N(≤105) and
C
C
C, where
N
N
N is the number of records and
C
C
C is the column that you are supposed to sort the records with. Then
N
N
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 ( ≤ 10 5 ) N (≤10^5) N(≤105) 和 C C C,其中 N N N 表示记录的数量, C C C 表示要求你排序所依据的列。随后 N N N 行,每行包含一条学生记录。一条学生记录由学生的 ID(一个 6 位数)、姓名(一个没有空格的、不超过 8 个字符的字符串),以及成绩(一个在 0 和 100 之间的整数,包括 0 和 100)。
Output Specification:
For each test case, output the sorting result in
N
N
N lines. That is, if
C
=
1
C = 1
C=1 then the records must be sorted in increasing order according to IDs; if
C
=
2
C = 2
C=2 then the records must be sorted in non-decreasing order according to names; and if
C
=
3
C = 3
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 N N 行输出。就是说,如果 C = 1 C = 1 C=1,那么记录必须按 ID 的升序排列;如果 C = 2 C = 2 C=2,那么按姓名的非降序排列;还有如果 C = 3 C = 3 C=3,那么记录必须按成绩的非降序排列。如果有几个学生的姓名或成绩相同,他们必须按他们的 ID 升序排列。
Sample Input 1:
3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
Sample Output 1:
000001 Zoe 60
000007 James 85
000010 Amy 90
Sample Input 2:
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
Sample Output 2:
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Sample Input 3:
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
Sample Output 3:
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90
2. 问题分析
2.1. 题型归类
简单的排序题,多条件排序,条件彼此没有太多复合;每个主要条件有一个排序结果,一个主要条件最多有一个附加条件;而且附加条件建立在主要条件的结果之上,仅在局部的排序起作用。
2.2. 输入分析
N
N
N 表示记录的数量,
C
C
C 表示要求你排序所依据的列。每条记录的结构是 ID name grade
,ID
是个 6 位整数,可用int
存储;name
是一个字符串,长度不大于 8 个字符,不含空格,可用 char name[10]
存储;grade
是一个整数,取值范围为
[
0
,
100
]
[0,100]
[0,100],为 int
型。
2.3. 输出分析
输出
N
N
N 条记录,每条记录的结构仍为 ID name grade
。
2.4. 流程分析
输入到输出的条件:要求程序能够按三种不同的排序规则将记录排序,“如果 C = 1 C = 1 C=1,那么记录必须按 ID 的升序排列;如果 C = 2 C = 2 C=2,那么按姓名的非降序排列;还有如果 C = 3 C = 3 C=3,那么记录必须按成绩的非降序排列。如果有几个学生的姓名或成绩相同,他们必须按他们的 ID 升序排列。”
3. 解题思路
分三种情况写 cmp
函数,使用 switch
语句,根据不同的 c
值,执行不同的 sort()
语句
4. 参考代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5;
struct student {
int ID;
char name[10];
int grade;
} students[N];
bool cmpID(student a, student b) {
if (a.ID != b.ID)
return a.ID < b.ID;
}
bool cmpName(student a, student b) {
if (strcmp(a.name, b.name) != 0)
return strcmp(a.name, b.name) < 0;
else
return a.ID < b.ID;
}
bool cmpGrade(student a, student b) {
if (a.grade != b.grade)
return a.grade < b.grade;
else
return a.ID < b.ID;
}
int main(){
int n, c;
scanf("%d %d", &n, &c);
for (int i = 0; i < n; i++)
scanf("%d %s %d", &students[i].ID, students[i].name, &students[i].grade);
switch (c) {
case 1: {
sort(students, students + n, cmpID);
break;
}
case 2: {
sort(students, students + n, cmpName);
break;
}
case 3: {
sort(students, students + n, cmpGrade);
break;
}
}
for (int i = 0; i < n; i++)
printf("%06d %s %d\n", students[i].ID, students[i].name, students[i].grade);
return 0;
}