A1006 Sign In and Sign Out (25 point(s))

排序,取最早(小)时间,最晚时间

1. 原文

原题

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:

1
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:

1
2
3
4
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

Sample output:

1
SC3021234 CS301133

2. 解析

取最早开门的时间,最晚关门时间。

通过cmp函数判断时间早晚后取值

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<cstdio>
#include<cstring>
struct node{
char id[20];
int h,m,s;
};
bool cmp(node a,node b){
if(a.h!=b.h){
return a.h>b.h;
}else if(a.m!=b.m){
return a.m>b.m;
}else{
return a.s>b.s;
}
}
int main(){
node max,min;
min.h = 24; min.m = 60; min.s = 60;
max.h = 0; max.m = 0; max.s = 0;
int n;
scanf("%d",&n);
for (int i = 0; i < n; ++i)
{
node temp;
scanf("%s%d:%d:%d",temp.id,&temp.h,&temp.m,&temp.s);
if (cmp(min,temp))
{
min = temp;
}
scanf("%d:%d:%d",&temp.h,&temp.m,&temp.s);
if (cmp(temp,max))
{
max = temp;
}
}
printf("%s %s\n",min.id,max.id);
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1006/
  • 发布时间: 2019-09-03 12:36
  • 更新时间: 2021-10-29 13:58
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!