题目

Excel can sort records according to any column. Now you are supposed to imitate this function.

Input Specification:

Each input file contains one test case. For each case, the first line contains two integers N (≤10​5 ) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then 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).

Output Specification:

For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID's; if C = 2 then the records must be sorted in non-decreasing order according to names; and if 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.

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

题目解读

给出N个学生信息和一个数字C,每个学生的信息包括ID(6位数字),姓名(长度最多为8的字符串,无空格),分数(0-100)。根据数字C的取值,对学生信息按照不同策略进行排序,最终输出排序后的学生信息:

C=1:按 ID 递增;

C=2:按姓名递增,如果同名,按ID递增;

C=3:按分数递增,如果同分,按ID递增。

思路分析

这不就是三种排序策略就完了吗?

  • 首先创建结构体 Student 保存学生信息,注意 name 字段不要用string!!!,否则你最后一个测试点会是 运行超时,实际是内存溢出!



    其实可以想想,题目给出说明姓名字段长度不超过10个字符,怎么可能没用呢,是吧!
  • 如何排序,因为我们使用sort()函数对整个结构体数组进行排序,自己实现的比较函数只能是这样int cmp(Student a, Student b),所以我们要把 C 定义成一个全局变量,然后在这个函数中根据 C的取值进行不同的逻辑实现,当然你也可以实现三个比较函数,但我觉得没有必要。

代码

题目比较简单,代码注释也挺详细,没什么好说的。有个地方需要注意:学号是6位数字,输出必须用printf("%06d", id)格式化。

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std; // 学生信息
struct Student {
int id, score;
// string name;
char name[10];
}stu[10001]; // 根据那个字段排序
int flag; // 自定义比较函数
int cmp(Student a, Student b) {
// 按照ID递增
if (flag == 1) return a.id < b.id;
// 按照姓名自增
else if (flag == 2) {
// 重名就比较ID
if (strcmp(a.name, b.name) == 0) return a.id < b.id;
return strcmp(a.name, b.name) <= 0;
} else {
// 按照分数递增,相同就比较ID
return a.score != b.score ? a.score < b.score : a.id < b.id;
}
} int main() { // N 个学生
int n;
// 根据哪个字段排序
cin >> n >> flag;
// 读入学生信息
for (int i = 0; i < n; ++i) {
// cin >> stu[i].id >> stu[i].name >> stu[i].score;
scanf("%d %s %d", &stu[i].id, stu[i].name, &stu[i].score);
}
// 排序
sort(stu, stu + n, cmp);
// 输出
for (int i = 0; i < n; ++i) {
// printf("%06d ", stu[i].id);
// cout << stu[i].name << " " << stu[i].score << endl;
printf("%06d %s %d\n", stu[i].id, stu[i].name, stu[i].score);
} return 0;
}

PAT 1028 List Sorting (25分) 用char[],不要用string的更多相关文章

  1. PAT 甲级 1028 List Sorting (25 分)(排序,简单题)

    1028 List Sorting (25 分)   Excel can sort records according to any column. Now you are supposed to i ...

  2. PAT (Advanced Level) Practice 1028 List Sorting (25 分) (自定义排序)

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...

  3. 【PAT甲级】1028 List Sorting (25 分)

    题意: 输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数).输出排序后的信 ...

  4. 1028 List Sorting (25 分)

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...

  5. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

    1052 Linked List Sorting (25 分)   A linked list consists of a series of structures, which are not ne ...

  6. PAT 1028 List Sorting[排序][一般]

    1028 List Sorting (25)(25 分) Excel can sort records according to any column. Now you are supposed to ...

  7. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

  8. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  9. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

随机推荐

  1. 基于tcp协议的套接字通信:远程执行命令

    要解决粘包问题: TCP:流式协议 特点: 1.数据流没有开头也没有结果,像水流一样 2.TCP协议有一个nagle算法, nagle算法会将数据量较小,并且时间间隔较短的数据合成一条数据发送, 这么 ...

  2. JZ2440 linux-3.4.2内核启动报错:Verifying Checksum ... Bad Data CRC

    使用的uboot版本是1.1.6,是打过u-boot-1.1.6_jz2440.patch的: kernel使用的版本是3.4.2, 也是打过linux-3.4.2_camera_jz2440.pat ...

  3. 全网最全最细的appium自动化测试环境搭建教程以及appium工作原理

    一.前言 ​ 对于appium自动化测试环境的搭建我相信90%的自学者都是在痛苦中挣扎,在挣扎中放弃,在放弃后又重新开始,只有10%的人,人品比较好,能够很快并顺利的搭建成功.appium 自动化测试 ...

  4. 如何快速地恢复你的win10

    win10清单 这份List不会介绍如何安装系统,而是当面对一个新系统,如何最快的搭建,或者说恢复到一个生产力环境. 必备习惯 备份软件安装包和常用内容上云是高效恢复的两点关键. 备份软件安装包 我的 ...

  5. 005.Ansible de palybook简单使用

    一 Ansible Playbook简介 ansbile-playbook是一系列ansible命令的集合,利用yaml 语言编写.playbook命令根据自上而下的顺序依次执行.同时,playboo ...

  6. php算--------法

    <?php //冒泡排序:两两交换数值,最小的值在最左边,就如最轻的气泡在最上边.对整列数两两交换一次//最小的数在最左边,每次都能得一个在剩下的数中的最小 的数//“冒”出来的数组成一个有序区 ...

  7. 【Python可视化】使用Pyecharts进行奥运会可视化分析~

    项目全部代码 & 数据集都可以访问我的KLab --[Pyecharts]奥运会数据集可视化分析-获取,点击Fork即可- 受疫情影响,2020东京奥运会将延期至2021年举行: 虽然延期,但 ...

  8. wireshark的基础认识

    简单的抓包分析 使用过滤功能: 数据分别经过:物理层-> 数据链路层->网络层 ->传输层 ->应用层 下面将详细的查分各个层所涉及的东西. 物理层:单位是比特流 数据链路层; ...

  9. Java反射详细介绍

    反射 目录介绍 1.反射概述 1.1 反射概述 1.2 获取class文件对象的三种方式 1.3 反射常用的方法介绍 1.4 反射的定义 1.5 反射的组成 1.6 反射的作用有哪些 2.反射的相关使 ...

  10. 【10月新版】Aspose.Pdf 10月新版V17.10发布 | 附下载

    2019独角兽企业重金招聘Python工程师标准>>> Aspose.Pdf for .NET 17.10 更新 功能和改进 核心 概述 类别 PDFNET-38067 支持DICO ...