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
解题思路: 文章大意是,记录每个人的名字,以及进入门,以及出门的时间,要求找到开门和锁门的人。
众所周知,开门的肯定是所有时间里面最早进入的人,关门的肯定就是所有时间里面最晚出来的人,所有这两个是不相关的两件事。因为一个人的信息有多种类型的数据,所以不妨设置一个结构体,一个结构体结点在本题相当于一条信息(名字--string,小时--int,分钟--int,秒--int),同时因为时间有进入与出去,不妨设置数组:
struct people
{
string name;
int hour[2];//[0]表示进入,[1]表示离开
int minute[2];//[0]表示进入,[1]表示离开
int second[2];//[0]表示进入,[1]表示离开};
存好后,依次比较小时、分钟、秒的数字大小,顺序为 如果hour已经小了,就没必要比较分钟了,直接把名字拷过来,在小时相等的情况下比较分钟的大小,以此类推:
源代码:
- #include<cstdio>
- #include<iostream>
- #include<string.h>
- using namespace std;
- struct people
- {
- string name;
- int hour[2];//[0]表示进入,[1]表示离开
- int minute[2];
- int second[2];
- };
- int main()
- {
- int n;
- int min_hour=100,min_minute=100,min_second=100;
- string min_name;
- int max_hour=0,max_minute=0,max_second=0;
- string max_name;
- cin>>n;
- people peo[15];
- for(int i=0;i<n;i++)
- {
- cin>>peo[i].name;
- scanf("%d:%d:%d",&peo[i].hour[0],&peo[i].minute[0],&peo[i].second[0]);
- scanf("%d:%d:%d",&peo[i].hour[1],&peo[i].minute[1],&peo[i].second[1]);
- }
- for(int i=0;i<n;i++)
- {
- if(peo[i].hour[0]<min_hour)
- {
- min_hour=peo[i].hour[0];
- min_name=peo[i].name;
- }
- else if(peo[i].hour[0]==min_hour)
- {
- if(peo[i].minute[0]<min_minute)
- {
- min_minute=peo[i].minute[0];
- min_name=peo[i].name;
- }
- else if(peo[i].minute[0]==min_minute)
- {
- if(peo[i].second[0]<min_second)
- {
- min_second=peo[i].second[0];
- min_name=peo[i].name;
- }
- else continue;
- }
- }
- //printf("%d:%d:%d",peo[i].hour[0],peo[i].minute[0],peo[i].second[0]);
- //printf("%d:%d:%d",peo[i].hour[1],peo[i].minute[1],peo[i].second[1]);
- if(peo[i].hour[1]>max_hour)
- {
- max_hour=peo[i].hour[1];
- max_name=peo[i].name;
- }
- else if(peo[i].hour[1]==max_hour)
- {
- if(peo[i].minute[1]>max_minute)
- {
- max_minute=peo[i].minute[1];
- max_name=peo[i].name;
- }
- else if(peo[i].minute[1]==max_minute)
- {
- if(peo[i].second[1]>max_second)
- {
- max_second=peo[i].second[0];
- max_name=peo[i].name;
- }
- else continue;
- }
- }
- }
- cout<<min_name<<" "<<max_name<<endl;
- }