PAT甲级 1006. Sign In and Sign Out(25分)

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

结尾无空行
#include <iostream>
using namespace std;

struct Time
{
    
    
    int hour;
    int minute;
    int second;
};

struct Person
{
    
    
    string id;
    Time in_time;
    Time out_time;
};

int main()
{
    
    
    int M;
    cin >> M;
    Person p;
    int early_in_time = 1e9, late_out_time = 0;
    string early_person, late_person;
    for (int i = 0; i < M; i++)
    {
    
    
        cin >> p.id;
        scanf("%d:%d:%d", &p.in_time.hour, &p.in_time.minute, &p.in_time.second);
        scanf("%d:%d:%d", &p.out_time.hour, &p.out_time.minute, &p.out_time.second);
        int intime = (p.in_time.hour * 60 + p.in_time.minute) * 60 + p.in_time.second;
        int outtime = (p.out_time.hour * 60 + p.out_time.minute) * 60 + p.out_time.second;
        if (intime < early_in_time)
        {
    
    
            early_in_time = intime;
            early_person = p.id;
        }
        if (outtime > late_out_time)
        {
    
    
            late_out_time = outtime;
            late_person = p.id;
        }
    }
    cout << early_person << " " << late_person;
    return 0;
}

很简单的题,但我的代码写的特别烂。完全没有必要写这么复杂,还结构里嵌套了结构,要搞清楚一点,求的其实是时间戳,何况是同一天的,只需要考虑一共占用了多少秒,再依次比较求出最值即可。感觉陷入了一种思维惯性,不仅仅要考虑优化算法,写法的优化也是不可缺少的。

猜你喜欢

转载自blog.csdn.net/leoabcd12/article/details/119546548
今日推荐