题目描述

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

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
5
6
7
8
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
2
3
4
5
SC3021234 CS301133



结尾无空行

柳婼题解

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
#include <iostream>
#include <climits>
using namespace std;
int main() {
int n, minn = INT_MAX, maxn = INT_MIN;
scanf("%d", &n);
string unlocked, locked;
for(int i = 0; i < n; i++) {
string t;
cin >> t;
int h1, m1, s1, h2, m2, s2;
scanf("%d:%d:%d %d:%d:%d", &h1, &m1, &s1, &h2, &m2, &s2);
int tempIn = h1 * 3600 + m1 * 60 + s1;
int tempOut = h2 * 3600 + m2 * 60 + s2;
if (tempIn < minn) {
minn = tempIn;
unlocked = t;
}
if (tempOut > maxn) {
maxn = tempOut;
locked = t;
}
}
cout << unlocked << " " << locked;
return 0;
}

自己的题解(少过一个测试用例)

不熟悉c++的语法和api 。。。。。。。。所以写得有点复杂

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// Created by 张旭辉 on 2021/8/15.
//
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

bool isEarly(string inTime,string earliest){
int hh = stoi(inTime.substr(0,2));
int mm = stoi(inTime.substr(3,2));
int ss = stoi(inTime.substr(6,2));

int eh = stoi(earliest.substr(0,2));
int em = stoi(earliest.substr(3,2));
int es = stoi(earliest.substr(6,2));

if(hh<eh){
// earliest = inTime;
return true;
}else if(hh==eh&&mm<em){
// earliest = inTime;
return true;
}else if(hh==eh&&mm==em&&ss<es){
// earliest = inTime;
return true;
}
return false;
}
bool isLatest(string outTime,string latest){
int hh = stoi(outTime.substr(0,2));
int mm = stoi(outTime.substr(3,2));
int ss = stoi(outTime.substr(6,2));

int lh = stoi(latest.substr(0,2));
int lm = stoi(latest.substr(3,2));
int ls = stoi(latest.substr(6,2));

if(hh>lh){
// latest = outTime;
return true;
}else if(hh==lh&&mm>lm){
// latest = outTime;
return true;
}else if(hh==lh&&mm==lm&&ss>ls){
// latest = outTime;
return true;
}
return false;
}
int main(){
string max;
string min;
string inName;
string outName;

int m;
string inTime,outTime,id;
scanf("%d" ,&m);
//scanf("%s %s %s", &id[0], &min[0], &max[0]);
cin>>id>>min>>max;
inName =id;
outName = id;
for(int i =1 ; i < m ; i++){
//scanf("%s %s %s",&id[0], &inTime[0], &outTime[0]);
cin>>id>>inTime>>outTime;
if(isEarly(inTime,min)){
inName = id;
};
if(isLatest(outTime,max)){
outName =id;
};

}

cout<<inName<<" "<<outName;

return 0;
}