List Sorting(PAT A 1028)

该博客介绍了一道编程题,要求模仿Excel的排序功能,对学生成绩表进行按ID、姓名或成绩的排序。输入包含学生记录数量N和排序依据列C,输出排序后的记录。题目提供了三个比较函数分别对应不同排序条件,并利用C++的sort函数进行排序。解题思路是根据C值选择合适的比较函数进行排序,最后输出排序结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分数值: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 gradeID 是个 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值