立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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。n条记录,排序, k=1则按学号升序排序,k=2则按名字升序排序,k=3则按成绩升序排序。 如果名字或成绩相等,则按学号升序排序

分析:主要考察自定义排序,具体逻辑见我的代码。


二更:网上有些同学用cin、cout导致超时。因此建议用c写。 其实加上ios::sync_with_stdio(false);这行代码后(取消流同步),C++反而比C要快一些。


#include<bits/stdc++.h>
using namespace std;struct recode{string id, name;int score;
}re[100010];
int n, c; bool cmp(recode r1, recode r2) {if(c == 1) return r1.id < r2.id;if(c==2) if(r1.name != r2.name) return r1.name < r2.name;else return r1.id < r2.id;else if(c == 3)if(r1.score != r2.score) return r1.score < r2.score;else return r1.id < r2.id;
}int main() {ios::sync_with_stdio(false); cin >> n >> c;for(int i = 0; i < n; i++) cin >> re[i].id >> re[i].name >> re[i].score;sort(re, re+n, cmp);for(int i = 0; i < n; i++) cout << re[i].id << ' ' << re[i].name << ' ' << re[i].score << '\n';return 0;
}

耗时:


       ——记住,不要愤怒,因为愤怒会降低你的智慧;也不要仇恨自己的敌人,因为仇恨会使你丧失判断力。与其恨自己的敌人,不如拿他来为我所用。

【简洁代码】1028 List Sorting (25 分)_26行代码AC相关推荐

  1. L2-1 包装机 (25 分)(STL43行代码)

    L2-1 包装机 (25 分) 一种自动包装机的结构如图 1 所示.首先机器中有 N 条轨道,放置了一些物品.轨道下面有一个筐.当某条轨道的按钮被按下时,活塞向左推动,将轨道尽头的一件物品推落筐中.当 ...

  2. 【附超时原因】1055 The World‘s Richest (25 分)_42行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Forbes magazine publishes every year its list of billionaires bas ...

  3. 【简便解法】1090 危险品装箱 (25分)_33行代码AC

    立志用最少的代码做最高效的表达 PAT乙级最优题解-->传送门 集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里.比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸. 本题 ...

  4. 【详细解析】1080 MOOC期终成绩 (25分)_45行代码AC

    立志用更少的代码做更高效的表达 PAT乙级最优题解-->传送门 对于在中国大学MOOC(http://www.icourse163.org/ )学习"数据结构"课程的学生,想 ...

  5. 【简洁代码】1071 小赌怡情 (15分)_22行代码

    立志用更少的代码做更高效的表达 Pat乙级最优化代码+题解+分析汇总-->传送门 常言道"小赌怡情".这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个 ...

  6. 案例4-1.6 树种统计 (25 分)_18行代码AC

    立志用最少的代码做最高效的表达 随着卫星成像技术的应用,自然资源研究机构可以识别每一棵树的种类.请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比. 输入格式: 输入首先给出正整数N(≤ ...

  7. 1097 Deduplication on a Linked List (25 分)_35行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a singly linked list L with integer keys, you are supposed ...

  8. 【题意+分析】1071 Speech Patterns (25 分)_27行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 People often have a preference among synonyms of the same word. F ...

  9. 【题意+分析】1067 Sort with Swap(0, i) (25 分)_24行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given any permutation of the numbers {0, 1, 2,-, N−1}, it is easy ...

最新文章

  1. 华为路由器ospf路由表解读_华为动态路由OSPF实例详解之多区域配置-华为路由器设置...
  2. JUC之CountDownLatch的源码和使用场景分析
  3. QCustomplot怎么实现对大数据量的自适应采样显示不卡顿
  4. Java集合框架总结(5)——Map接口的使用
  5. 猫哥教你写爬虫 002--作业-打印皮卡丘
  6. Silverlight中枚举并加载客户端程序集
  7. Q96:PT(3):基于噪声的纹理(Noise-Based Textures)(0)——概述
  8. java.lang.ClassFormatError
  9. 生信软件的下载与使用方法总结
  10. DAX计算月末一次加权平均出库金额
  11. 收到谷歌实习邀请 “比被清华录取还激动”
  12. Apeaksoft iOS Toolkit for Mac(iOS设备数据恢复软件)
  13. 如何求子网掩码,默认网关地址,网络地址
  14. 在 cmd 中启动 Android 模拟器
  15. 2022-11-26 linux 通过blocking_notifier_chain_register、blocking_notifier_call_chain 接收温度变化信息控制风扇
  16. 【信号完整性】信号反射原理
  17. WCDMA通信技术-CFN SFN
  18. BEVDet: High-Performance Multi-Camera 3D Object Detection in Bird-Eye-View
  19. 全网详细介绍nginx的反向代理、正向代理配置,location的指令说明,反向代理的两个示例代码以及全局块,events块和http快的说明。
  20. CVT变速器中壳体吊机设计

热门文章

  1. scrapy框架的概念和流程
  2. kubernetes(六)k8s核心组件学习
  3. 新版本秒级自动部署,K8S才是永远的容器之神!
  4. 使用VMware VSphere WebService SDK进行开发 (二)——获取虚拟机cpu的使用情况
  5. 单调栈之Next Greater Number
  6. 音视频技术开发周刊 | 170
  7. LiveVideoStack线上分享第三季(七):AVS3关键技术介绍、性能和复杂度分析
  8. NIUDAY 11.23 北京站抢票啦 | 看 AI 落地行业 享 AI 时代红利
  9. 十月多媒体技术人聚会北京 LiveVideoStackCon 2018开启讲师/出品人招募
  10. Go 如何实现热重启