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

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

题目的输出是输出所有人中最早开门的和最晚锁门的人

解法一:很容易想到的是ID_number Sign_in_time Sign_out_time作为一个结构体的元素,
struct info{
ID_number
Sign_in_time
Sign_out_time
}

vector<info> v;
将所有的信息存入容器中,利用sort函数,先根据Sign_in_time从小到大排序,输出第一个ID_number,再根据Sign_out_time从大到小排序,输出第一个ID_number。

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 struct info{
 7     string ID_number;
 8     string Sign_in_time;
 9     string Sign_out_time;
10 };
11 bool unlock(info lhs,info rhs){
12     return lhs.Sign_in_time<rhs.Sign_in_time;
13 }
14 bool lock(info lhs,info rhs){
15     return lhs.Sign_out_time>rhs.Sign_out_time;
16 }
17 int main(int argc,char **argv){
18     info I;
19     string ID_number,Sign_in_time,Sign_out_time;
20     string unlock_ID,lock_ID;
21     vector<info> vi;
22     int N;
23     cin>>N;
24     for(int i=0;i<N;i++){
25         cin>>ID_number>>Sign_in_time>>Sign_out_time;
26         I.ID_number=ID_number;I.Sign_in_time=Sign_in_time;I.Sign_out_time=Sign_out_time;
27         vi.push_back(I);
28     }
29     std::sort(vi.begin(),vi.end(),unlock);
30     unlock_ID=(*vi.begin()).ID_number;
31     std::sort(vi.begin(),vi.end(),lock);
32     lock_ID=(*vi.begin()).ID_number;
33     cout<<unlock_ID<<" "<<lock_ID<<endl;
34     return 0;
35 }

C++字符串比较很给力!!

解法二:我只要所有人中Sign_in_time最小和Sign_out_time最大的,所以初始化所有人中unlock_time=”23:59:59“,lock_time=”00:00:00“

每输入一个ID_number Sign_in_time Sign_out_time,若Sign_in_time <= unlock_time,则unlock_time = Sign_in_time, In_ID_number  = ID_number,

若Sign_out_number >= lock_time,则lock_time = Sign_out_time, Out_ID_number  = ID_number,

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 int main(){
 6     char mins[16],maxs[16], str[16];
 7     char minT[10] = "23:59:59";
 8     char maxT[10] = "00:00:00";
 9     char tmp_in[10], tmp_out[10];
10     int num, i;
11 
12     cin >> num;
13     for(i = 0; i < num; i++){
14         cin >> str >> tmp_in >> tmp_out;
15         if(strcmp(tmp_in, minT) <= 0){
16             strcpy(mins, str);
17             strcpy(minT, tmp_in);
18         }
19         if(strcmp(tmp_out, maxT) >= 0){
20             strcpy(maxs, str);
21             strcpy(maxT, tmp_out);
22         }
23     }
24     cout << mins << " " << maxs << endl;
25     return 0;
26 }
 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 using namespace std;
 5 
 6 int main(){
 7     string mins, maxs, str;
 8     string minT = "23:59:59";
 9     string maxT = "00:00:00";
10     string tmp_in, tmp_out;
11     int num, i;
12 
13     cin >> num;
14     for(i = 0; i < num; i++){
15         cin >> str >> tmp_in >> tmp_out;
16         if(tmp_in <= minT){
17             mins = str;
18             minT= tmp_in;
19         }
20         if(tmp_out >= maxT){
21             maxs = str;
22             maxT = tmp_out;
23         }
24     }
25     cout << mins << " " << maxs << endl;
26     return 0;
27 }

C++字符串比较很给力!!

以前比较时间(   时:分:秒  )总是先比较小时,再比较分钟,最后比较秒,现在发现可以直接把它当做字符串,通过比较字符串来比较时间的先后。


原文地址:https://www.cnblogs.com/qinduanyinghua/p/5709332.html