【PAT】1028.List Sorting

【PAT】1028.List Sorting

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 9

Sample Output 3:

000002 James 9
000001 Zoe 60
000007 James 85
000010 Amy 90

题解

根据C的值是1还是2还是3,对相应的列排序。第一列(学号)升序,第二列(姓名)不降序,第三列(成绩)不降序。注:姓名或者成绩相同时根据学号升序排序。

import java.util.*;public class Solution2 {//学生结构体public static class Student {int id;String name;int grade;Student(int id, String name, int grade) {this.id = id;this.name = name;this.grade = grade;}}public static void main(String[] args) {List<Student> Stulist = new ArrayList<>();int N, C;Scanner in = new Scanner(System.in);N = in.nextInt();C = in.nextInt();//输入学生信息for (int i = 0; i < N; i++) {int id = in.nextInt();String name = in.next();int grade = in.nextInt();Stulist.add(new Student(id, name, grade));}//根据C 的三种不同排序方法if (C == 1) {sort(1, Stulist);}else if (C == 2) {sort(2, Stulist);}else {sort(3, Stulist);}//输出排序后的的Listfor (int i = 0; i < N; i++) {Student stu = Stulist.get(i);//按格式输出System.out.printf("%06d %s %d", stu.id, stu.name, stu.grade);System.out.println();}}public static void sort(int C, List<Student> list) {if (C == 1) {Collections.sort(list, new comparator1());}else if (C == 2) {Collections.sort(list, new comparator2());}else {Collections.sort(list, new comparator3());}}//return 1:交换位置//return -1:不交换位置// return o1-o2:升序排列public static class comparator1 implements Comparator<Student> {@Overridepublic int compare(Student o1, Student o2) {return (o1.id - o2.id) <= 0 ? -1 : 1;}}public static class comparator2 implements Comparator<Student> {@Overridepublic int compare(Student o1, Student o2) {if (o1.name.equals(o2.name)) {return (o1.id - o2.id) <= 0 ? -1 : 1;}return o1.name.compareTo(o2.name);}}public static class comparator3 implements Comparator<Student> {@Overridepublic int compare(Student o1, Student o2) {if (o1.grade == o2.grade) {return (o1.id - o2.id) <= 0 ? -1 : 1;}return (o1.grade - o2.grade <= 0) ? -1 : 1;}}
}

结果

测试点提示内存(KB)用时(ms)结果得分
017168115
答案正确
5 / 5
116476118
答案正确
5 / 5
216564118
答案正确
5 / 5
316972118
答案正确
2 / 2
416916113
答案正确
2 / 2
516744116
答案正确
2 / 2
651104400
运行超时
0 / 4
部分正确 分数 21 / 25,有一个测试点超时。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/4001.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql的完全包含关系怎么写

问&#xff1a; mysql从a表查到aid有两值1&#xff0c;2&#xff0c;b表中存在a表的主键作为外键&#xff0c;从b表中查找完全包含aid的的值&#xff08;1&#xff0c;2&#xff09;的bid 答 如果你有一个表a包含主键列a_id&#xff0c;并且有一个表b&#xff0c;它具有一个…

数据结构和算法:深度优先搜索 (DFS) 和广度优先搜索 (BFS) 相关题目

文章目录 1. 岛屿问题&#xff08;岛屿连通&#xff09;1.1 岛屿数量1.1.1 DFS 解法1.1.2 BFS 解法 深度优先搜索 (DFS) 和广度优先搜索 (BFS)是比较难的算法问题&#xff0c;但也是面试常考题&#xff0c;因此需要认真研究并掌握。 DFS 用递归实现&#xff0c;BFS用栈实现 1. …

使用 SageMaker 对 Whisper 模型进行微调及部署

使用 SageMaker 对 Whisper 模型进行微调及部署 Whisper 作为 OpenAI 最新开源的自动语音识别&#xff08;ASR&#xff09;模型&#xff0c;采用了编码器-解码器&#xff08;encoder- decoder&#xff09;transformer架构&#xff0c;并使用了 68 万小时的从互联网收集的多语言…

PersistentVolume:怎么解决数据持久化

Kubernetes 的 Volume 对数据存储已经给出了一个很好的抽象&#xff0c;它只是定义了有这么一个“存储卷”&#xff0c;而这个“存储卷”是什么类型、有多大容量、怎么存储&#xff0c;我们都可以自由发挥。Pod 不需要关心那些专业、复杂的细节&#xff0c;只要设置好 volumeMo…

安卓APK反编译+修改+重打包+签名

目录 1.下载反编译工具包。2.将APK包&#xff0c;重命名为ZIP&#xff0c;解压。放到反编译根目录下。3.使用apktool反编译修改smail文件&#xff0c;进行重打包4.重新打包5.重签名 1.下载反编译工具包。 反编译工具包地址&#xff1a;百度网盘 提取码&#xff1a;dsu3 解压后…

AtcoderABC243场

A - Shampoo A - Shampoo ] 题目大意 高桥家有三个人&#xff1a;高桥、他的父亲和他的母亲。每个人每晚都在浴室洗头发。他们按照顺序使用AA、BB和CC毫升的洗发水。 问&#xff0c;今天早上瓶子里有VV毫升的洗发水。在不重新装满的情况下&#xff0c;谁会第一个用完洗发水洗头…

网工内推 | 美图秀秀招网工,大专以上,15薪,NP认证优先

01 美图公司 招聘岗位&#xff1a;网络工程师 职责描述&#xff1a; 1、美图大厦网络、分公司网络、IT相关项目的网络、办公内网服务器&#xff1b; 2、负责网络的设计、运行、管理和维护等工作&#xff1b; 3、负责远程办公环境的优化、运行、管理和维护工作&#xff1b; 4、…

python匿名函数Lambda

1、Lambda 函数简介 Lambda函数也被称为匿名(没有名称)函数&#xff0c;它直接接受参数的数量以及使用该参数执行的条件或操作&#xff0c;该参数以冒号分隔&#xff0c;并返回最终结果。为了在大型代码库上编写代码时执行一项小任务&#xff0c;或者在函数中执行一项小任务&a…

git merge详细用法

git merge用法 一、开发分支&#xff08;dev&#xff09;上的代码达到上线的标准后&#xff0c;要合并到 master 分支 git checkout dev git pull git checkout master git merge dev git push -u origin master二、当master代码改动了&#xff0c;需要更新开发分支&#xff…

吴恩达机器学习2022-Jupyter-用scikitlearn实现线性回归

1可选实验:使用Scikit-Learn进行线性回归 有一个开源的、商业上可用的机器学习工具包&#xff0c;叫做 scikit-learn。本工具包包含您将在本课程中使用的许多算法的实现。 1.1工具 您将利用 scikit-learn 以及 matplotlib 和 NumPy 中的函数。 2线性回归封闭式解决方案 Sc…

nvm常用命令

查看node.js的所有版本&#xff1a; npm view node versions 查看 nvm 是否安装成功&#xff1a; command -v nvm查看当前使用的 Node 版本&#xff1a; node -v将安装 12.7.0 版本的Node&#xff1a; nvm install 12.7.0卸载 12.7.0 版本的Node&#xff1a; nvm uninstall 12…

为什么低代码只能掀起小浪花?了解低代码的得失与前景

导语&#xff1a;低代码是相对于高代码和无代码的一个中间概念&#xff0c;通常强调的是用户不需要学习如何写代码&#xff0c;就能完成工作。然而低代码模式一直不温不火&#xff0c;原因是什么呢&#xff1f;一起来看一下吧。 最近互联网大公司裁员消息又起&#xff0c;“低代…

Linux 配置常见服务器命令

Linux常见配置服务器的命令整理&#xff0c;基于Centos 7 。 基本操作命令 查看ens33 ip ifconfig ens33 查看系统版本 cat /etc/redhat-release 查找文件install.log、httpd.conf、hosts、smb.conf、network find -name te.txt 使用“cp 源文件 目标文件”命令进行复制 cp t…

Serial/TCP/NTRIP通信

1 串口通信 串口通讯 串口通信详解 串口通信中的4大参数含义 Qt 串口通信的简单demo 2 TCP通信 TCP协议简介 TCP协议详细介绍 TCP协议(全面) IP地址和端口号的详解 3 NTRIP通信 Ntrip通讯协议1.0 什么是Ntrip&#xff1f;Ntrip协议简介 TCP高并发数据转接服务器&#xff0…

【ElasticSearch】java 如何连接 elasticsearch

不同版本的java 与 Elastic 如何连接。 在 es 与 java 连接中最重要的就是『兼容性矩阵」&#xff0c;要严格按照兼容性矩阵的要求来部署 spring boot 或者 es的版本&#xff0c;否则会有意向不到的错误或者程序无法运行等等。 针对 es 6.2.2&#xff0c;java 客户端的配置代…

Linux: cannot read file data

报错&#xff1a; Could not load library libcudnn_cnn_infer.so.8. Error: /home/qc/miniconda3/envs/DNAqc/lib/python3.10/site-packages/torch/lib/libcudnn_cnn_infer.so.8: cannot read file data Please make sure libcudnn_cnn_infer.so.8 is in your library path! A…

【NLP】国外新动态--LLM模型

一、说明 NLP走势如何?这是关于在实践中使用大型语言模型(LLM)的系列文章中的一篇文章。在这里,我将介绍LLM,并介绍使用它们的3个级别。未来的文章将探讨LLM的实际方面,例如如何使用OpenAI的公共API,Hugging Face Transformers Python库,如何微调LLM,以及如何从头开始…

navicate_windows_14

1.新建文本文档2.输入如下内容 echo off set dnInfo set dn2ShellFolder set rpHKEY_CURRENT_USER\Software\Classes\CLSID :: reg delete HKEY_CURRENT_USER\Software\PremiumSoft\NavicatPremium\Registration14XCS /f %针对<strong><font color"#FF0000"…

Android 进阶之旅 - 终章

我们的《Android进阶之旅》到这里就算是完结了&#xff0c;五六年的时间&#xff0c;我的青春&#xff0c;我职业生涯的黄金时期&#xff0c;基本都耗在了这上面。很多东西我都是现学现卖&#xff0c;平时上班也很少有时间能备课&#xff0c;很多地方难免讲得不如意&#xff0c…

ChatGpt+人工修正 PyQt5 实现简易视频播放器

支持功能: 1. 视频播放速度调整 2. 视频声音调整 3. 视频当前播放帧截帧(用的ffmpeg 怎么装自己百度去,截取准确度很高,QT自带的截帧那玩意信号不触发,不好使) 4. 视频暂停 # !/usr/bin/env python3 # -*- coding: UTF-8 -*- """ @author : v_jiaohaich…