题目:
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个上下班的打卡时间。上班最早的负责开门,下班最晚的负责关门。输出开门和关门的员工id,用空格隔开。
题目保证同一个员工的上班时间早于下班时间。题目保证员工不会同时上班或下班(即不会出现相同的时间)。
答案:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
bool early_late(int* store, int* tim)//前者早false,后者早true
{
if (store[0] < tim[0])//小时
{
return false;
}
else if (store[0] > tim[0])
{
return true;
}
else
{
if (store[1] < tim[1])//分钟
{
return false;
}
else if (store[1] > tim[1])
{
return true;
}
else
{
if (store[2] < tim[2])//秒
{
return false;
}
else
{
return true;//题目保证时间不会相同
}
}
}
}
int main()
{
int early[3] = { 24,60,60 }, late[3] = { 0,0,0 };//初始化最早时间最晚时间
char early_id[20], late_id[20];//最早id与最晚id
int tim[3];
char id[20];
int m;
char temp;//用来存储:
cin >> m;
for (int i = 0; i < m; i++)
{
cin >> id;
cin >> tim[0] >> temp >> tim[1] >> temp >> tim[2];
if (early_late(early, tim))
{
for (int j = 0; j < 3; j++)
{
early[j] = tim[j];
}
memcpy(early_id, id, sizeof(id));
}
cin >> tim[0] >> temp >> tim[1] >> temp >> tim[2];
if (!early_late(late, tim))
{
for (int j = 0; j < 3; j++)
{
late[j] = tim[j];
}
memcpy(late_id, id, sizeof(id));
}
}
cout << early_id << " " << late_id;
}