您的位置:首页 > 其它

PAT1006. Sign In and Sign Out (25)

2018-03-16 11:12 323 查看

问题 PAT1006. 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
4000
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


题意理解

给定几个人的编号、进入时间、离开时间,输出最早进入的人和最晚离开的人的编号。

问题分析

这道题很容易给出思维定势的答案,即我们在判断时首先判断时,然后判断分、秒,对人类来说这可能是十分方便的算法,但是相对于算法编写却十分复杂。其实这个问题有个十分简单的方法:将整个时间表看作一个坐标轴,以今天的0时刻为起点,计算给出的每个时间点所对应的秒数,数目最小的即最早进入的,数目最大的即最晚离开的。

代码

时间复杂度为O(n),空间复杂度为O(n)。使用了数据结构,不使用的话代码更简洁些。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstring>
#include <map>
#include <climits>

using namespace std;

//data structure
typedef struct Person{
int ID;
char ID_number[20];
int Sign_in_time;
int Sign_out_time;
}Person;

int main(int argc, char *argv[]) {

int m;
Person p[100];
int unlockedPerson;
int unlockedTime = 0x3f3f3f3f;
int lockedPerson;
int lockedTime = 0;

//freopen("Input.txt","r",stdin);

cin >> m;

for(int i = 0;i < m;i++){
char data[20];
int hour,minute,second;

p[i].ID = i;
cin >> p[i].ID_number;

//why there is a Segment error when I use this code ??
/*
while(data
!= ' '){
p[i].ID_number
= data
;
n++;
}
*/

cin >> hour;getchar();
cin >> minute;getchar();
cin >> second;getchar();
p[i].Sign_in_time = hour * 60 * 60 + minute * 60 + second;

cin >> hour;getchar();
cin >> minute;getchar();
cin >> second;getchar();
p[i].Sign_out_time = hour * 60 * 60 + minute * 60 + second;

}
//choose the one who have unlocked the door on that day
for(int i = 0;i < m;i++){
if(unlockedTime > p[i].Sign_in_time){
unlockedTime = p[i].Sign_in_time;
unlockedPerson = i;
}
}

//choose the one who have locked the door on that day
for(int i = 0;i < m;i++){
if(lockedTime < p[i].Sign_out_time){
lockedTime = p[i].Sign_out_time;
lockedPerson = i;
}
}

cout << p[unlockedPerson].ID_number << ' ' << p[lockedPerson].ID_number;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: