PAT A1006 Sign In and Sign Out

前言

传送门

正文

题目描述

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

解题思路:

题意就是找到最早签到和最晚签离的人,首先可用一个结构体person来存储姓名和小时,分钟,秒。设置结构体型变量start,end来分别存储最早签到和最晚签离的人的信息,而temp则存储每次读入的人的信息。这里需要注意temp可以先读入签到时间,与start比较完后,再读入签离时间,与end比较。最终start就是最早签到的人的信息,end就是最晚签离的人的信息。
(后面很多题都有用到temp临时变量来存储读入的每项数据,然后再通过compare来不断更新题目最终结果要求的数据项)

参考题解:

#include<cstdio>
struct person{
	char name[16];
	int hh,mm,ss;
}start,end,temp;
\\a的时间若比b的时间晚则返回true
bool cmp(person a,person b){
	if(a.hh!=b.hh)return a.hh>b.hh;
	if(a.mm!=b.mm)return a.mm>b.mm;
	return a.ss>b.ss;
}
void init(){
	end.hh=0;end.mm=0;end.ss=0;\\设置初始签离时间为最早
	start.hh=23;start.mm=59;start.ss=59;\\设置初始签到时间为最晚
}
int main(){
	init();
	int n;
	scanf("%d",&n);
	while(n--){
		scanf("%s %d:%d:%d",temp.name,&temp.hh,&temp.mm,&temp.ss);
		if(!cmp(temp,start))start=temp;
		scanf("%d:%d:%d",&temp.hh,&temp.mm,&temp.ss);
		if(cmp(temp,end))end=temp;
	}
	printf("%s %s\n",start.name,end.name);
	return 0;
} 

后记

初次尝试GuiMiner挖矿

发布了65 篇原创文章 · 获赞 101 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_40563761/article/details/102957657