1. 程式人生 > >1006 Sign In and Sign Out (25)

1006 Sign In and Sign Out (25)

sca div 表示 spec ins input ted nta person

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

大意大意:給出每個人的進出時間,找出第一個進入和最後一個離開的
思路:用map<int, string>來保存數據, key表示進出時間,把時間化作秒作為關鍵詞, value記錄人的名字;因為在map中插入數據的時候,會按照key排序,所以用map保存數據後,map中第一個保存的就是最先進入的, 最後一個保存的就是最後離開的;
用scanf()能很方便的得到進出的時間

 1 #include<iostream>
 2 #include<map>
 3 #include<string>
 4 using namespace std;
 5 int main(){
 6   int n, i;
 7   cin>>n;
 8   map<int, string> mmap;
 9   for(i=0; i<n; i++){
10     int hr1, min1, sec1, hr2, min2, sec2;
11     string name;
12     cin>>name;
13     scanf("%d:%d:%d %d:%d:%d", &hr1, &min1, &sec1, &hr2, &min2, &sec2);
14     mmap[hr1*3600+min1*60+sec1]=name;
15     mmap[hr2*3600+min2*60+sec2]=name;
16   }
17   auto it=mmap.end();
18   cout<<mmap.begin()->second<<" "<<(--it)->second<<endl;
19   return 0;
20 }








1006 Sign In and Sign Out (25)