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

简介: 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;
}

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

目录
相关文章
|
安全 Linux 测试技术
|
测试技术 Linux 内存技术
怎样设置freeswitch 的高并发数
怎样设置freeswitch 的高并发数
|
9月前
|
传感器 数据采集 数据可视化
基于STM32的智能家居控制系统设计
基于STM32的智能家居控制系统设计
999 10
|
9月前
|
机器学习/深度学习 人工智能 算法
孪生素数分布的大规模计算实验:分段筛实现与经验统计分析
孪生素数猜想断言存在无穷多对形如 (p,p+2)(p,p+2) 的素数,但该猜想至今仍未解决。Hardy–Littlewood 的第一猜想给出了孪生素数计数函数的经典启发式渐近公式,而张益唐、Maynard 与 Polymath8 的工作则证明了存在无穷多对素数,其间隔有统一上界,但这一上界尚未降至 2 [1–3]。
832 13
|
9月前
|
JSON 监控 Java
亿级流量系统 JVM 全链路调优:从压测瓶颈定位到线上稳态落地全指南
本文系统讲解亿级流量下JVM调优的工程化实践,涵盖压测基准建设、瓶颈根因定位、JDK17内存模型与GC原理、代码级优化(避免短生命周期/大对象、规范ThreadLocal)、G1/ZGC参数调优、OS内核优化及线上稳态保障,强调“先代码、后配置、再参数”的科学路径。
734 2
|
9月前
|
人工智能 自然语言处理 安全
用了几个月的心得,教你把AI开源知识库用透
在知识管理领域混迹多年,尝试过各类系统和助手,这次总结一下经验教训。AI开源知识库系统的核心价值,在于务实解决实际问题,而非堆砌花哨功能,能有效改善知识创作、检索与协作中的低效困境。下文结合我的实操经验,详细分享该系统的部署方法、核心功能用法、落地案例及避坑技巧,全程聚焦干货,不冗余、不浮夸。
|
网络协议 安全 物联网
《复杂环境下IPv6地址规划与子网划分全攻略》
随着网络环境日益复杂及设备数量激增,IPv6以其超大地址空间和即插即用优势成为网络变革的关键。本文深入探讨IPv6地址规划与子网划分的挑战与方法,包括夯实基础知识、遵循层次性与高效性原则、应对物联网与云计算等特殊场景需求,以及验证优化的重要性。通过合理规划与科学划分,可构建高效、稳定且安全的IPv6网络,为未来发展奠定基础。
1155 19
|
算法
面试场景题:如何设计一个抢红包随机算法
本文详细解析了抢红包随机算法的设计与实现,涵盖三种解法:随机分配法、二倍均值法和线段切割法。随机分配法通过逐次随机分配金额确保总额不变,但易导致两极分化;二倍均值法优化了金额分布,使每次抢到的金额更均衡;线段切割法则将总金额视为线段,通过随机切割点生成子金额,手气最佳金额可能更高。代码示例清晰,结果对比直观,为面试中类似算法题提供了全面思路。
2492 16
|
程序员
神奇的「小黄鸭调试法」
什么意思?就是说,当你被一段代码整不会了,怎么看也看不出问题出在哪里的时候。你可以对着一只小黄鸭,一行一行解释你的代码。不仅解释代码的功能,你最好还要告诉鸭子你到底想实现什么目标,以及你打算通过什么样的方法实现,为什么要这么实现等等。当你说着说着说着,哎,突然你就明白问题在哪了。就是这么的神奇。
2265 0
|
编译器 C语言
【C语言】宏定义详解
宏定义(Macro Definition)是C语言预处理器的一部分,通过`#define`指令引入。宏定义在编译前的预处理阶段进行文本替换,即将代码中的宏名替换为定义的内容。
3735 6