C++这个程序怎么写

每天开始时,在机房里签到的第一个人会开门,最后一个签出的人会锁门。鉴于登录和注销的记录,您应该找到当天解锁并锁定门的人。

输入

每个输入文件包含一个测试用例。每个事例都包含一天的记录。该事例以正整数 M 开头,这是记录的总数,后跟 M 行,每行的格式为:

ID_number Sign_in_time Sign_out_time
其中时间以 HH:MM:SS 格式给出,ID 号是不超过 15 个字符的字符串。

输出

对于每个测试用例,在一行中输出当天解锁并锁定门的人员的 ID 号。两个 ID 号必须用一个空格分隔。

注意:可以保证记录是一致的。也就是说,签到时间必须早于每个人的签出时间,并且没有两个人同时登录或注销。

样例输入

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

样例输出

SC3021234 CS301133

题目来源



#include<stdio.h>
#include<string.h>
#include <math.h>
int main()
{
    int M = 0;
    scanf("%d",&M);
    char str[100][3][15];
    for (int i = 0; i < M; i++)
    {
        scanf("%s%s%s",&str[i][0], &str[i][1], &str[i][2]);
    }
    char Sign_in_time[15];
    char Sign_out_time[15];
    strcpy(Sign_in_time,str[0][1]);
    strcpy(Sign_out_time, str[0][2]);
    int num_in = 0;
    int num_out = 0;
    for (int i = 0; i < M; i++)
    {
        if (strcmp(Sign_in_time,str[i][1])>0)
        {
            strcpy(Sign_in_time, str[i][1]);
            num_in = i;
        }
        if (strcmp(Sign_out_time, str[i][2]) < 0)
        {
            strcpy(Sign_out_time, str[i][2]);
            num_out = i;
        }
    }
    printf("%s %s", str[num_in][0], str[num_out][0]);
    return 0;
}

// g++ -Wall -std=c++17 main.cpp

#include <iostream>
#include <string>
#include <tuple>
#include <cassert>
#include <vector>

class Time
{
public:
    Time() = default;
    Time(int hour, int minute, int second) : _hour(hour), _minute(minute), _second(second)
    {
        assert(hour >= 0 && hour < 24);
        assert(minute >= 0 && minute < 60);
        assert(second >= 0 && second < 60);
    }

    int hour() const { return _hour; }
    int minute() const { return _minute; }
    int second() const { return _second; }

    bool operator<(const Time &other) const
    {
        if (_hour < other._hour)
            return true;
        if (_hour > other._hour)
            return false;
        if (_minute < other._minute)
            return true;
        if (_minute > other._minute)
            return false;
        return _second < other._second;
    }

    bool operator>(const Time &other) const
    {
        return other < *this;
    }

private:
    int _hour = 0;
    int _minute = 0;
    int _second = 0;
};

template <typename CharT, typename Traits>
std::basic_istream<CharT, Traits> &operator>>(std::basic_istream<CharT, Traits> &is, Time &time)
{
    int hour = 0, minute = 0, second = 0;
    char ch;
    is >> hour >> ch >> minute >> ch >> second;
    time = Time(hour, minute, second);
    return is;
}

template <typename CharT, typename Traits>
std::basic_istream<CharT, Traits> &operator<<(std::basic_ostream<CharT, Traits> &os, const Time &time)
{
    os << time.hour() << ':' << time.minute() << ':' << time.second();
    return os;
}

int main()
{
    int n;
    std::cin >> n;
    std::vector<std::tuple<std::string, Time, Time>> records(n);
    for (int i = 0; i < n; i++)
    {
        std::string id_number;
        Time sign_in_time;
        Time sign_out_time;
        std::cin >> id_number >> sign_in_time >> sign_out_time;
        records[i] = std::make_tuple(id_number, sign_in_time, sign_out_time);
    }

    Time min_time(23, 59, 59);
    Time max_time(0, 0, 0);
    std::string unlock_id;
    std::string lock_id;
    for (const auto &[id_number, sign_in_time, sign_out_time] : records)
    {
        if (sign_in_time < min_time)
        {
            min_time = sign_in_time;
            unlock_id = id_number;
        }
        if (sign_out_time > max_time)
        {
            max_time = sign_out_time;
            lock_id = id_number;
        }
    }

    std::cout << unlock_id << ' ' << lock_id << '\n';

    return 0;
}