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 和一些正整数,请你从中选择尽可能多的数构成一个完美数列. 输入格 ...
随机推荐
- js array flat all in one
js array flat all in one array flat flatMap flatMap > flat + map https://developer.mozilla.org/en ...
- shit 牛客网
shit 牛客网 为什么,只可以 log 一次,什么垃圾逻辑呀! https://www.nowcoder.com/test/question/e46437833ddc4c5bb79f7af7a1b7 ...
- github & markdown & image layout
github & markdown & image layout css & right https://github.com/sindresorhus/log-symbols ...
- how to recursively all files in a folder with sudo permissions in macOS
how to recursively all files in a folder with sudo permissions in macOS write bug OK sudo chmod 777 ...
- NGK和USDN的应用
一.NGK和USDN的发展方向 目前区块链将会朝着两个方向去发展,第一种是金融经济的衍生品,第二种是商业应用,快速支付的货币体系,NGK.IO公链是基于分布式应用设计的商用金融区块链操作系统,通过数字 ...
- 牛市下SPC新空投糖果来了
2021年元旦刚过没几天,比特币就开启了牛市的旅程,比特币涨至三万四千美元,率领众多币种暴涨,一股浓浓的牛市味道来了. 虽然从昨天开始,比特币带领着主流币进行了一波调整,但是只涨不跌的市场是 大家说比 ...
- np.mean(img, axis=(0, 1))
np.mean(img, axis=(0, 1)) img 是shape为(H,W,3)的图片 np.mean(img, axis=(0, 1)) 是求出各个通道的平均值,shape是 (3, ) ...
- C++单链表反转、两有序链表合并仍有序
1 #include<iostream> 2 3 struct Node 4 { 5 int data; 6 Node *next; 7 }; 8 9 typedef struct Nod ...
- 五分钟快速上手MyBatis
MyBatis 是一款优秀的持久层框架,它支持自定义 SQL.存储过程以及高级映射. 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作. 可以通过简单的 XML 或注解来配置和映射,Ja ...
- mybatis 一对多和多对一 简单案例笔记
以案例说明(以下案例代码都敲过验证过) 多对一(多个学生对一个老师 即学生集合中都存一个老师对象) Mybatis多对一实现方式1: //定义Student 和 Teacher 实体 @Data p ...