PAT 1028 List Sorting (25分) 用char[],不要用string
题目
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 (≤105 ) 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的更多相关文章
- PAT 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- 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 ...
- 【PAT甲级】1028 List Sorting (25 分)
题意: 输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数).输出排序后的信 ...
- 1028 List Sorting (25 分)
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- PAT 1028 List Sorting[排序][一般]
1028 List Sorting (25)(25 分) Excel can sort records according to any column. Now you are supposed to ...
- PTA PAT排名汇总(25 分)
PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
随机推荐
- react引入图片不显示问题
在react 中引入图片的方式和正常不同,,很容易引入不显示 引入本地图片 1.(采用组件式引入方法) import Logo from "图片路径" <img src={L ...
- POJ2044 天气预报---状态细则
墙角数枝梅,凌寒独自开. 遥知不是雪,为有暗香来.--王安石 题目:天气预报 网址:http://poj.org/problem?id=2044 你是一个可以控制降雨的神仙. 你是一个仁慈的神,希望土 ...
- 使用Spring Boot搭建你的第一个应用程序
文章目录 依赖配置 main程序配置 MVC配置 安全配置 存储 Web 页面和Controller 异常处理 测试 结论 Spring Boot是Spring平台的约定式的应用框架,使用Spring ...
- Ali_Cloud++:阿里云服务器部署【禅道】项目管理系统
1.开源版安装包下载 地址一:百度云下载 10.0 提取码:2dyg 地址二:官方下载 2.直接解压安装包到/opt目录下 注意:这里我安装的是Linux一键安装包官方给出的方法就是直接解压到/o ...
- JS编程建议——11:慎重使用伪数组
建议11:慎重使用伪数组JavaScript没有真正的数组,因此typeof运算符不能辨别数组和对象.伪数组在JavaScript中有很高的易用性,程序员不用给它设置维度,而且永远不用担心产生越界错误 ...
- 解决iframe跨域刷新的问题
用iframe的location.reload(true); 方法来刷新外部URL会报 Blocked a frame with origin xxxx from accessing a cross- ...
- python(os 模块)
1.os.name 输出字符串指示正在使用的平台.如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix' import os print(os.name) #结果如下 ...
- muduo网络库源码学习————条件变量
muduo里的CountDownLatch类实际上是对条件变量condition进行的封装,既可以用于所有子线程等待主线程发起 "起跑" ,也可以用于主线程等待子线程初始化完毕才开 ...
- 解决Vue中文本输入框v-model双向绑定后数据不显示的问题
前言 项目中遇到一个问题就是在Vue中双向绑定对象属性时,手动赋值属性后输入框的数据不实时更新的问题. <FormItem label="地址" prop="eve ...
- window下用notepad++编辑了脚本文件然后放在linux报错显示无法运行
首先vi :set ff 查看文件类型 接着 下载dos2unix root用户下yum -y install dos2unix 然后 dos2unix 文件.sh 转换格式 接着在正常启动即可