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
结尾无空行
题意:
给出M,输入M行,每行包括一个学生的id和该学生到达的时间以及他离开的时间,最后要求输出所有学生中最早到的人和最晚走的人的id值。
分析:
- 建立结构体STU来接收学生的id和到达和离开的时间,因为最后只需要输出最早和最晚,所以在接收时可以直接进行比较,舍弃不需要的值。
- 创建判断的方法res,true为最早到,false为最晚走,将最早和最晚的值保存到in和out中。
- 输出最早和最晚的id值。
相关知识点:
- 接收学生姓名用%s,加上头文件#include
- 判断最早,最晚时用,bool res(STU s1,STU s2)(true为最早到,false为最晚走)记得加上头文件和命名空间
代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct STU{
char id[20];
int h,m,s;//时间
}in,out,temp;
bool res(STU s1,STU s2){//true为最早到,false为最晚走
if(s1.h!=s2.h) return s1.h<s2.h;
if(s1.m!=s2.m) return s1.m<s2.m;
else return s1.s<s2.s;
}
int main(){
int k;
scanf("%d",&k);
in.h=24,in.m=60,in.s=60;
out.h=0,out.m=0,out.s=0;
for(int i=0;i<k;i++){
scanf("%s %d:%d:%d",&temp.id,&temp.h,&temp.m,&temp.s);
if(res(temp,in)) in=temp;
scanf("%d:%d:%d",&temp.h,&temp.m,&temp.s);
if(res(temp,out)==false) out=temp;
}
printf("%s %s",in.id,out.id);
return 0;
}