Home Pat 1478 签到与签出
Post
Cancel

Pat 1478 签到与签出

题目

每天第一个到机房的人负责开门,最后一个从机房离开的人负责锁门。

现在,给定每个人的签到与签出记录,请你找出当天开门的人以及锁门的人分别是谁。

输入格式

第一行包含整数 M,表示共有 M 个人的签到签出记录。

接下来 M 行,每行的形式如下:

1
ID_number Sign_in_time Sign_out_time

时间以 HH:MM:SS 形式给出,ID_number 是一个长度不超过 15 的字符串。

输出格式

共一行,输出开门人和锁门人的ID_number,用一个空格隔开。

数据范围

1≤M≤10, 数据保证每个人的签到时间早于签出时间,并且不会出现两个人同时签到或同时签出的情况。

输入样例:

1
2
3
4
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

输出样例:

1
SC3021234 CS301133


题解

  1. 用字典序直接比较时间大小 (string直接比较)

    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
    
     #include <iostream>
     using namespace std;
        
     int GetTimeInSecond(string str)
     {
         int h = (str[0] - '0') * 10 + (str[1] - '0');
         int m = (str[3] - '0') * 10 + (str[4] - '0');
         int s = (str[6] - '0') * 10 + (str[7] - '0');
         return h * 3600 + m * 60 + s;
     }
        
     int main()
     {
         int n;
         cin >> n;
         string  signInId, signOutId;
         int signInTime = 999999, signOutTime = 0;
         for (int i = 0; i < n; ++i) {
             string id, signIn, signOut;
             cin >> id >> signIn >> signOut;
             int time = GetTimeInSecond(signIn);
             if (time < signInTime) {
                 signInTime = time;
                 signInId = id;
             }
             time = GetTimeInSecond(signOut);
             if (time > signOutTime) {
                 signOutTime = time;
                 signOutId = id;
             }
         }
         cout << signInId << ' ' << signOutId;
         return 0;
     }
    
  2. 题解2

    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
    
     #include <iostream>
     using namespace std;
        
     int main()
     {
         int n;
         cin >> n;
         string open_id, open_time;
         string close_id, close_time;
        
         for (int i = 0; i < n; ++i) {
             string  id, in_time, out_time;
             cin >> id >> in_time >> out_time;
        
             if (!i || in_time < open_time) {
                 open_id = id;
                 open_time = in_time;
             }
             if (!i || out_time > close_time) {
                 close_id = id;
                 close_time = out_time;
             }
         }
            
         cout << open_id << ' ' << close_id;
         return 0;
     }
        
    
This post is licensed under CC BY 4.0 by the author.
Contents