PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642
PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642
题目描述:
At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.
译:在每一天的开始,第一个在机房签到的人开门,最后一个签退的人关门。给你签到和签退人的记录,你应该找到该天中谁开的门和谁锁的门。
Input Specification (输入说明):
Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:
ID_number Sign_in_time Sign_out_time
where times are given in the format HH:MM:SS, and ID_number is a string with no more than 15 characters.
译:每个输入文件包含一个测试用例,每个测试用例包含一天的记录。这个用例以一个正整数 M 开始,表示总的记录条数。接下来 M 行,格式如下:
ID_number Sign_in_time Sign_out_time
给出的时间格式为:HH:MM:SS , 并且 ID_number 是一个不超过 15 个字符的字符串。
Output Specification (输出说明):
For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.
Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.
译:对于每个测试用例,在一行中输出这一天中开门的人和锁门的人的 ID number 。这两个 ID number 之间必须用一个空格分开。
注意:题目保证记录是符合实际的,意味着每个人的签到的时间必须比签退的时间更早,并且没有两个人同时签到或者签退。
Sample Input (样例输入):
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
Sample Output (样例输出):
SC3021234 CS301133
The Idea:
第一种方法: 很明显的是使用结构体的排序。首先就想到了 sort 。对于sort的排序是很熟悉了,所以一次就 AC 了 。
第二种方法:由于题目中并没有指出 M 的具体范围(写者首先在第一种方法中内置 MAX 为 1000000,结果爆内存了),所以就想着使用动态添加记录的。而需要排序,故选择了 map (map 既能按照 key 升序降序排列也能按照 value 升序降序排,为了简单且题目中说明 签到时间和签退时间不会重复,所以可以利用时间作为 key 值,ID_number 作为 value 值,这样就可以分别拿到 签到时间升序 和签退时间降序的 第一个key的 value 值就是本题答案。)
The Codes —— sort:
#include<bits/stdc++.h>
using namespace std ;
#define MAX 10010
typedef struct node{
string name ;
string inTime ;
string outTime ;
}Node ;
Node peo[MAX] ;
int m ;
bool cmp1(Node a , Node b){
return a.inTime < b.inTime ; // 直接比较字符串的大小 , cmp1 表示按照签到时间先后排序
}
bool cmp2(Node a ,Node b){
return a.outTime > b.outTime ; // cmp2 表示按照签退时间倒序排序
}
int main(){
cin >> m ;
getchar() ;
for(int i = 0 ; i < m ; i ++){
cin >> peo[i].name >> peo[i].inTime >> peo[i].outTime ;
}
sort(peo , peo + m , cmp1);
string s1 = peo[0].name ;
sort(peo , peo + m , cmp2) ;
s1 += " " + peo[0].name ;
cout << s1 << endl ;
return 0 ;
}
The Codes —— map:
#include<bits/stdc++.h>
using namespace std;
map<string,string,less<string> > in ; // 按照 关键字升序排列的 map
map<string,string,greater<string> > out ; // 按照 关键字降序排列的 map
int m ;
string name , inTime , outTime , ans ;
int main(){
cin >> m ;
getchar() ; // 吸收输入 m 之后的 换行符
for(int i = 0 ; i < m ; i ++){
cin >> name >> inTime >> outTime ;
in[inTime] = name ; // 建立 签到时间 到 ID_number 的 map
out[outTime] = name ; // 建立 签退时间 到 人名 的 map
}
map<string , string ,less<string> >::iterator it = in.begin() ;// 关键字升序迭代器
map<string , string , greater<string> >::iterator it2 = out.begin() ;// 关键字降序迭代器
ans = it -> second ; // 最早签到的 ID_number
ans += " " + it2 -> second ; // 最晚签退的 ID_number
cout << ans << endl ;
return 0 ;
}
PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642的更多相关文章
- PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642 题目描述 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下 ...
- PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642 题目描述: "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大 ...
- PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 目录 PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) ...
- PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 凌宸1642
PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 题目描述 本题的基本要求非常简单:给定 N 个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的 ...
- PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...
- PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642
PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...
- PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) (排序)
At the beginning of every day, the first person who signs in the computer room will unlock the door, ...
- PAT (Advanced Level) Practice 1002 A+B for Polynomials 分数 25
This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: Each ...
- PAT (Basic Level) Practice (中文)1030 完美数列 (25 分) (有点意思)
给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列. 输入格 ...
随机推荐
- 三维码 & 二维码 & 一维码
三维码 & 二维码 & 一维码 3D, 2D, 1D 防伪国家标准 -<结构三维码防伪技术条件> http://www.xinhuanet.com/tech/2019-12 ...
- Learning JavaScript with MDN & 使用 MDN 学习 JavaScript
Learning JavaScript with MDN & 使用 MDN 学习 JavaScript Learn JavaScript with MDN 和 MDN 一起学习 JavaScr ...
- macOS & Nginx
macOS & Nginx ngnix # 使用 brew 安装(如果没有 brew 命令,需要自行安装 brew) $ brew install nginx $ nginx -h # 查看 ...
- NGK官方又出助力市场计划方案 1万枚VAST任性送
近期NGK官方的一系列动作,可以说是在向外界宣告:NGK2.0即将来袭,席卷加密数字货币市场.前一段时间,NGK官方宣布,NGK公链布局算力领域,打造NGK算力生态星空计划,并推出了SPC星空币.目前 ...
- python进阶(1)Lambda表达式
Lambda表达式 lambda表示的是匿名函数,不需要用def来声明,一句话就可以声明出一个函数 语法 函数名 = lambda 参数:返回值 注意点 1.函数的参数可以有多个,多个参数之间用逗号隔 ...
- List转String数组 collection.toArray(new String[0])中new String[0]的语法解释
Collection的公有方法中,toArray()是比较重要的一个. 但是使用无参数的toArray()有一个缺点,就是转换后的数组类型是Object[]. 虽然Object数组也不是不能用,但当你 ...
- vue中将分号去掉,将双引号变为单引号的配置
在项目根目录下创建.prettierrc文件,文件内容如下: { "semi": false, "singleQuote": true } 实现vs code中 ...
- 几种常见css布局
单列布局 第一种 给定宽度,margin:auto 即可实现 html <div class="header"></div> <div class=& ...
- 00.从0实现一个JVM语言系列
00.一个JVM语言的诞生 由于方才才获悉博客园文章默认不放在首页的, 原创文章主要通过随笔显示, 所以将文章迁移到随笔; 这篇帖子将后续更新, 欢迎关注! 这段时间要忙着春招实习, 所以项目更新会慢 ...
- 9.Vue之webpack打包基础---模块化思维
主要内容: 1. 什么是模块化思维? 2. ES6包的封装思想 一.什么是模块化思维呢? 现实工作中, 一个项目可能会有多个人同时开发. 然后, 将所有人开发的内容, 合并到一个文件中. 比如: 1 ...