1006 Sign In and Sign Out (25 point(s))

1006 Sign In and Sign Out (25 point(s))

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.

Example:

#include<iostream>
#include<cstdio>
#include<vector>

using namespace std;

struct Record {
    string ID;
    struct {
        int HH;
        int MM;
        int SS;
    } In;
    struct {
        int HH;
        int MM;
        int SS;
    } Out;
};

int main()
{
    int M;
    cin >> M;
    if(M == 0) return 0;
    vector<Record> record(M);
    for(int i = 0; i < M; i++) {
        int HH, MM, SS;
        cin >> record[i].ID;
        scanf("%d:%d:%d", &HH, &MM, &SS);
        record[i].In.HH = HH;
        record[i].In.MM = MM;
        record[i].In.SS = SS;
        scanf("%d:%d:%d", &HH, &MM, &SS);
        record[i].Out.HH = HH;
        record[i].Out.MM = MM;
        record[i].Out.SS = SS;
    }
    Record *first, *last;
    first = last = &record[0];
    for(int i = 1; i < M; i++) {
        if(record[i].In.HH < first->In.HH) first = &record[i];
        else if(record[i].In.HH == first->In.HH) {
            if(record[i].In.MM < first->In.MM) first = &record[i];
            else if(record[i].In.MM == first->In.MM) {
                if(record[i].In.SS < first->In.SS) first = &record[i];
            }
        }
        if(record[i].Out.HH > last->Out.HH) last = &record[i];
        else if(record[i].Out.HH == last->Out.HH) {
            if(record[i].Out.MM > last->Out.MM) last = &record[i];
            else if(record[i].Out.MM == last->Out.MM) {
                if(record[i].Out.SS > last->Out.SS) last = &record[i];
            }
        }
    }
    cout << first->ID << ' ' << last->ID;
}

Ideas:

Use the array to pick the maximum and minimum values.

Guess you like

Origin blog.csdn.net/u012571715/article/details/113898235