PAT A1006 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

题意:

给定计算机教室里每个人员的ID号、签到和签退时间,求出第一个签到和最后一个签退的人员的ID.

输入样式:

人数M
ID号 签到时间 签退时间
. . .

输出样式:

第一个签到人员的ID 最后一个签退人员的ID

思路:

(1)设定一个结构体persons存放人员的ID号,签到时间,签退时间;
(2)设定比较函数cmp1按照签到的时间升序对person[0]至person[M-1]排序,输出第一个签到人员person[0]的ID;
(3)设定比较函数cmp2按照签退的时间降序对person[0]至person[M-1]排序,输出最后一个签退人员person[0]的ID;

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
struct persons{
	char ID_number[16];
	int Sign_in_time[3],Sign_out_time[3];
}person[1000000000];
bool cmp1(persons a,persons b){//按照签到的时间升序排序的比较函数 
	if(a.Sign_in_time[0]!=b.Sign_in_time[0])
		return a.Sign_in_time[0]<b.Sign_in_time[0];
	else if(a.Sign_in_time[1]!=b.Sign_in_time[1])
		return a.Sign_in_time[1]<b.Sign_in_time[1];
	else if(a.Sign_in_time[2]!=b.Sign_in_time[2])
		return a.Sign_in_time[2]<b.Sign_in_time[2];
}
bool cmp2(persons a,persons b){//按照签退的时间降序排序的比较函数 
	if(a.Sign_out_time[0]!=b.Sign_out_time[0])
		return a.Sign_out_time[0]>b.Sign_out_time[0];
	else if(a.Sign_out_time[1]!=b.Sign_out_time[1])
		return a.Sign_out_time[1]>b.Sign_out_time[1];
	else if(a.Sign_out_time[2]!=b.Sign_out_time[2])
		return a.Sign_out_time[2]>b.Sign_out_time[2];
}
int main(){
	int M;
	scanf("%d",&M);
	for(int i=0;i<M;i++){
		scanf("%s %d:%d:%d %d:%d:%d",person[i].ID_number,
		&person[i].Sign_in_time[0],&person[i].Sign_in_time[1],&person[i].Sign_in_time[2],
		&person[i].Sign_out_time[0],&person[i].Sign_out_time[1],&person[i].Sign_out_time[2]);
	}
	sort(person,person+M,cmp1);//按照签到的时间升序排序
	printf("%s ",person[0].ID_number);
	sort(person,person+M,cmp2);//按照签退的时间降序排序
	printf("%s\n",person[0].ID_number);
	return 0;
}

词汇:

note 注意
algorithm 算术

发布了26 篇原创文章 · 获赞 0 · 访问量 488

猜你喜欢

转载自blog.csdn.net/PanYiAn9/article/details/102491403