PAT 1006 Sign In and Sign Out (25分)

Topic link: click here

Question: Every morning, the first person to sign in in the computer room opens the door, and the last person to sign in locks the door. According to the log in and out, you should be able to find the person who opened and closed the door that day.

Ideas: The time hh:mm:ssis converted into seconds, while traversing the m out record, the id corresponding to the minimum time and maximum time corresponding to the id, the answer to the output were recorded.

AC code:

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>

using namespace std;

int main()
{
    
    
    int m;
    scanf("%d", &m);
    
    string id, first_id, last_id;
    int hh, mm, ss, first_in = 1e9, last_out = -1e9;
    
    for(int i = 0; i < m; i++)
    {
    
    
        cin >> id;
        
        scanf("%d:%d:%d", &hh, &mm, &ss);
        int t = hh * 3600 + mm * 60 + ss;
        if(t < first_in)
        {
    
    
            first_in = t;
            first_id = id;
        }
        
        scanf("%d:%d:%d", &hh, &mm, &ss);
        t = hh * 3600 + mm * 60 + ss;
        if(t > last_out)
        {
    
    
            last_out = t;
            last_id = id;
        }
    }
    
    cout << first_id << " " << last_id << endl;
    
    return 0;
}

The WeChat public account "Algorithm Competition Job Search" is dedicated to explaining in detail the algorithm principles and templates involved in the competition and job search. Welcome to pay attention and communicate and progress together!

Guess you like

Origin blog.csdn.net/qq_42815188/article/details/108974124