版权声明:我的GitHub:https://github.com/617076674。真诚求星! https://blog.csdn.net/qq_41231926/article/details/83932873
我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED
原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805516654460928
题目描述:
题目翻译:
1006 签入签出
在每天的开始,第一个登录计算机房的人将打开门,最后一个登出的人将锁门。 给出登录和退出的记录,你需要找到那天开门和锁门的人。
输入格式:
每个输入文件包含一个测试用例。 每个测试用例包含一天的记录。 案例以正整数M开头,它是记录的总数,后跟M行,每行的格式如下:
ID_number Sign_in_time Sign_out_time
时间以形式HH:MM:SS给出,ID_number是一个不超过15个字符的字符串。
输出格式:
对于每个测试用例,在一行中输出当天开门和锁门的人的ID号。两个ID号必须用一个空格分隔。
注意:题目保证记录一致。也就是说,每个人的签入时间一定早于其签出时间,并且在同一时刻没有两个人登录或退出。
输入样例:
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
输出样例:
SC3021234 CS301133
知识点:时间的读取
思路:将时间转换为秒来比较先后
时间复杂度是O(M)。空间复杂度是O(1)。
C++代码:
#include<iostream>
#include<cstring>
using namespace std;
int change(int hour, int minute, int second);
int main(){
int M;
scanf("%d", &M);
char earliest[16], latest[16], temp[16];
int first = 86400;
int last = 0;
int hour, minute, second;
for(int i = 0; i < M; i++){
scanf("%s", temp);
scanf("%d:%d:%d", &hour, &minute, &second);
if(change(hour, minute, second) < first){
first = change(hour, minute, second);
strcpy(earliest, temp);
}
scanf("%d:%d:%d", &hour, &minute, &second);
if(change(hour, minute, second) > last){
last = change(hour, minute, second);
strcpy(latest, temp);
}
}
printf("%s %s\n", earliest, latest);
return 0;
}
int change(int hour, int minute, int second){
return hour * 60 * 60 + minute * 60 + second;
}
C++解题报告: