PTA | 1006 Sign In and Sign Out
1006 Sign In and Sign Out
作者 CHEN, Yue
单位 浙江大学
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开始,它是记录的总数,后面是M行,每行的格式是:
ID_number登录时间退出时间
其中时间以HH:MM:SS格式给出,ID_number是不超过15个字符的字符串。
输出规格:
对于每个测试用例,在一行中输出当天解锁和锁门的人员的ID号。两个身份证号码必须用一个空格分隔。
注:保证记录一致。也就是说,每个人的签到时间必须早于登出时间,并且不存在两个人同时签到或登出。
当完整的浏览一遍题目之后,我们可以大致提取出:
1, 机房签到和签退系统,要求是找出第一个签到的人和最后一个签退的人
2, 循环输入,并且不会存在同一时间签到或签退的人
3, 输入内容为:代号id,签到时间,签退时间,是典型的字符串处理题型
分析完题目之后,现在是手搓代码时间!
首先,根据题目要求,输入的签到记录为无序输入,同时由于只需要找到最早签到和最晚签退的那个人,因此顺序影响不大,又由于输入为字符串,故本题是典型的字符串处理问题,在本题中,我们只需要对字符串进行大小比较即可,因此属于字符串处理问题中的简单题。
废话不多说,解题思路如下:
1,定义变量对输入内容进行接收
2, 为了方便比较,我们将初始化四个变量,即最早签到的时间,最早签到的id,最晚签退的时间,最晚签退的id,其中,对于最早/最晚签到的时间,可以考虑将其初始化为第一个输入数据,也可以考虑初始化为“99:99:99”和“00:00:00”,此时为了避免初始化带来的偶然性影响,当考虑初始化为第一个输入数据时,应该同时考虑将对应的id一并初始化。
3, 输入和初始化处理完成之后,将进入本题的核心内容部分:字符串大小比较,关字符串大小比较机制,不明白的童鞋可以参考其他博主的文章视频,这里不再赘述,比较完成之后,直接格式化输出即可。
C++部分代码如下:
#include<bits/stdc++.h>
using namespace std;
int m;
string s1,s2,s3;
string fst="99:99:99",lst="00:00:00",fst_n, lst_n;
int main(){
cin>>m;
for(int i=0; i<m; i++){
cin>>s1>>s2>>s3;
// 字符串比较
if(s2<fst){
fst = s2;
fst_n = s1;
}
if(s3>lst){
lst = s3;
lst_n = s1;
}
}
cout<< fst_n <<" " << lst_n;
}
python部分代码:
nums = int(input())
lst = []
for i in range(nums):
temp = input().split()
lst.append(temp)
# 初始化
fst = lst[0][1]
lst_ = lst[0][2]
fst_name = lst[0][0]
lst_name = lst[0][0]
# 字符串比较
for i in lst:
if i[1] < fst:
fst = i[1]
fst_name = i[0]
if i[2] > lst_:
lst_ = i[2]
lst_name = i[0]
print(fst_name+" "+lst_name)
最后附上AK截图:
python:
C++:
写在后面:
本题难度较低,采用的解题方法比较直接,关键在于字符串匹配部分的基础知识,如果您对以上内容存在意见或者疑问,或者有更好的解题方式,欢迎评论区交流!