PAT 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, 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.
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.
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
题目解析
早上来的最早的人要开门,晚上离开的最晚的人要锁门。
给出M个人的编号 到达时间 离开时间
输出 开门的那个人的编号 锁门的那个人的编号
这题真没啥意思,不就是一个排序或者说比大小。
千万不要去创建结构体,真没必要,我们只需要编号,所以直接用两个变量保存负责开门的编号,负责关门的编号,在读入这些人信息的时候直接做比较,根据到达时间判断改变这个两个变量就可以了。
至于怎么保存“时间”以及比较,你可以把它转为以00:00:00开始的秒数,但我比较懒,直接用string类对象比较,> < 就可以搞定,只是比较字符串可能比比较整数速度慢,但是影响不大。
代码
#include <iostream>
#include <string>
using namespace std;
int main() {
// m个员工
int m;
cin >> m;
// 开锁的员工,上锁的员工
string unlock_no, lock_no;
// 初始化开锁时间,上锁时间
string unlock_time = "23:59:59", lock_time = "00:00:00";
// 当前员工编号,到达时间,离开时间
string emp_no, sign_in_time, sign_out_time;
for (int i = 0; i < m; ++i) {
cin >> emp_no >> sign_in_time >> sign_out_time;
// 最早来的那个人开锁,通过string类对象比较方法
if (sign_in_time < unlock_time) {
unlock_time = sign_in_time;
unlock_no = emp_no;
}
// 最晚走的那个人锁门
if (sign_out_time > lock_time) {
lock_time = sign_out_time;
lock_no = emp_no;
}
}
// 开锁的 锁门的
cout << unlock_no << " " << lock_no;
return 0;
}
// 将时间转为秒进行比较
// int main() {
// int n, minn = INT_MAX, maxn = INT_MIN;
// scanf("%d", &n);
// string unlocked, locked;
// for(int i = 0; i < n; i++) {
// string t;
// cin >> t;
// int h1, m1, s1, h2, m2, s2;
// scanf("%d:%d:%d %d:%d:%d", &h1, &m1, &s1, &h2, &m2, &s2);
// int tempIn = h1 * 3600 + m1 * 60 + s1;
// int tempOut = h2 * 3600 + m2 * 60 + s2;
// if (tempIn < minn) {
// minn = tempIn;
// unlocked = t;
// }
// if (tempOut > maxn) {
// maxn = tempOut;
// locked = t;
// }
// }
// cout << unlocked << " " << locked;
// return 0;
// }
PAT 1006 Sign In and Sign Out (25分) 字符串比较的更多相关文章
- PAT甲级:1036 Boys vs Girls (25分)
PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- PAT 乙级 1080 MOOC期终成绩 (25 分)
1080 MOOC期终成绩 (25 分) 对于在中国大学MOOC(http://www.icourse163.org/ )学习“数据结构”课程的学生,想要获得一张合格证书,必须首先获得不少于200分的 ...
- pat 1002 A+B for Polynomials (25 分)
1002 A+B for Polynomials (25 分) This time, you are supposed to find A+B where A and B are two polyno ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***
1066 Root of AVL Tree (25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...
- PAT 甲级 1055 The World's Richest (25 分)(简单题,要用printf和scanf,否则超时,string 的输入输出要注意)
1055 The World's Richest (25 分) Forbes magazine publishes every year its list of billionaires base ...
- PAT 甲级 1047 Student List for Course (25 分)(cout超时,string scanf printf注意点,字符串哈希反哈希)
1047 Student List for Course (25 分) Zhejiang University has 40,000 students and provides 2,500 cou ...
- PAT 甲级 1039 Course List for Student (25 分)(字符串哈希,优先队列,没想到是哈希)*
1039 Course List for Student (25 分) Zhejiang University has 40000 students and provides 2500 cours ...
- shell练习--PAT试题1010:一元多项式求导 (25 分)(失败案例喜加一)
---恢复内容开始--- 1010 一元多项式求导 (25 分) 设计函数求一元多项式的导数.(注:xn(n为整数)的一阶导数为nxn−1.) 输入格式: 以指数递降方式输入多项式非零项系 ...
随机推荐
- python学习笔记(五)---函数与类
函数 def为定义函数的一个标志 demo1: def greet_user(username): print("Hello, " + username.title() + &qu ...
- 虚拟化KVM之概述(一)
云计算基本概述 云计算是一种按使用量付费的模式,这种模式提供可用的.便捷的.按需的网络访问,进入可配置的计算资源共享池(资源包括网络,服务器,存储,应用程序,服务),这些资源能够被快速提供,只需投入很 ...
- [New Portal]Windows Azure Web Site (3) 创建Web Site和云端数据库
<Windows Azure Platform 系列文章目录> 在前一章的内容里,我介绍了使用Windows Azure Management Portal创建Web Site.本章,我将 ...
- 业务SQL那些事--慎用LIMIT
业务SQL那些事--慎用LIMIT 在业务中使用LIMIT限制SQL返回行数是很常见的事情,但如果不知道其中可能的坑或者说真正执行逻辑,就可能会使SQL执行非常慢,严重影响性能. LIMIT OFFS ...
- 再砸4.35亿美元,LG疯狂扩建太阳能电池生产线
LG在收缩高分辨率电视和其他消费电子产品业务的同时,在太阳能面板业务上却很明显一直在进行扩张.LG公司表示,他们将斥资4.35亿美元在韩国工厂增加超过6条生产线,使其太阳能电池生产量能够在2018年达 ...
- VB中使用字典存储类对象
2019独角兽企业重金招聘Python工程师标准>>> NODE类 Public pNext As NODE Public pPrev As NODE Public data As ...
- 数据库SQL语言从入门到精通--Part 2--MySQL安装
数据库从入门到精通合集(超详细,学习数据库必看) 首先给出的简单安装方式,文末给出标准的安装方式. 第一步下载我的压缩包 链接:https://pan.baidu.com/s/1EE40dU0j2U1 ...
- ACM-ICPC 2019 山东省省赛 A Calandar
这个题,呃完全的送分题,签到题,一周只有五天,一年12个月,一个月30天,公式为((year1-year2)*360%5+(month1-month2)*30%5+day1-day2+初始星期)%5, ...
- P2620 虫洞
题目背景 applepi 想进行宇宙旅行.当然,applepi 知道这是有可能的,因为applepi 的特殊能力能使他观测到宇宙中的虫洞.所谓虫洞就是一个在三维之外的维度打开的快捷通道,通过虫洞能够从 ...
- pycharm 新建文件后选错文件格式怎么改
经常在新建文件的时候,忘记填写文件后缀,导致文件无默认格式,而且同名字的文件怎么改都改不成想要的格式,所以随手记录一下怎么修正: 原因:肯定是pycharm已经默认指定了一个格式,所以再重复新建同样名 ...