PAT1006. Sign In and Sign Out (25)

问题 PAT1006. 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

题意理解

给定几个人的编号、进入时间、离开时间,输出最早进入的人和最晚离开的人的编号。

问题分析

这道题很容易给出思维定势的答案,即我们在判断时首先判断时,然后判断分、秒,对人类来说这可能是十分方便的算法,但是相对于算法编写却十分复杂。其实这个问题有个十分简单的方法:将整个时间表看作一个坐标轴,以今天的0时刻为起点,计算给出的每个时间点所对应的秒数,数目最小的即最早进入的,数目最大的即最晚离开的。

代码

时间复杂度为O(n),空间复杂度为O(n)。使用了数据结构,不使用的话代码更简洁些。

#include <iostream>
#include <cstdio>
#include <cstdlib> 
#include <algorithm>
#include <queue>
#include <stack>
#include <cstring>
#include <map>
#include <climits>

using namespace std;

//data structure
typedef struct Person{
    int ID;
    char ID_number[20];
    int Sign_in_time;
    int Sign_out_time;
}Person;

int main(int argc, char *argv[]) {

    int m;
    Person p[100];
    int unlockedPerson;
    int unlockedTime = 0x3f3f3f3f;
    int lockedPerson;
    int lockedTime = 0;

    //freopen("Input.txt","r",stdin);

    cin >> m;

    for(int i = 0;i < m;i++){
        char data[20];
        int hour,minute,second; 

        p[i].ID = i;
        cin >> p[i].ID_number;

        //why there is a Segment error when I use this code ??
        /*
        while(data[n] != ' '){
            p[i].ID_number[n] = data[n];
            n++;
        }
        */

        cin >> hour;getchar();
        cin >> minute;getchar();
        cin >> second;getchar();
        p[i].Sign_in_time = hour * 60 * 60 + minute * 60 + second;

        cin >> hour;getchar();
        cin >> minute;getchar();
        cin >> second;getchar();
        p[i].Sign_out_time = hour * 60 * 60 + minute * 60 + second;

    }
    //choose the one who have unlocked the door on that day
    for(int i = 0;i < m;i++){
        if(unlockedTime > p[i].Sign_in_time){
            unlockedTime = p[i].Sign_in_time;
            unlockedPerson = i;
        }
    }

    //choose the one who have locked the door on that day
    for(int i = 0;i < m;i++){
        if(lockedTime < p[i].Sign_out_time){
            lockedTime = p[i].Sign_out_time;
            lockedPerson = i;
        }
    }

    cout << p[unlockedPerson].ID_number << ' ' << p[lockedPerson].ID_number;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
东南亚位于我国倡导推进的“一带一路”海陆交汇地带,作为当今全球发展最为迅速的地区之一,近年来区域内生产总值实现了显著且稳定的增长。根据东盟主要经济体公布的最新数据,印度尼西亚2023年国内生产总值(GDP)增长5.05%;越南2023年经济增长5.05%;马来西亚2023年经济增速为3.7%;泰国2023年经济增长1.9%;新加坡2023年经济增长1.1%;柬埔寨2023年经济增速预计为5.6%。 东盟国家在“一带一路”沿线国家中的总体GDP经济规模、贸易总额与国外直接投资均为最大,因此有着举足轻重的地位和作用。当前,东盟与中国已互相成为双方最大的交易伙伴。中国-东盟贸易总额已从2013年的443亿元增长至 2023年合计超逾6.4万亿元,占中国外贸总值的15.4%。在过去20余年中,东盟国家不断在全球多变的格局里面临挑战并寻求机遇。2023东盟国家主要经济体受到国内消费、国外投资、货币政策、旅游业复苏、和大宗商品出口价企稳等方面的提振,经济显现出稳步增长态势和强韧性的潜能。 本调研报告旨在深度挖掘东南亚市场的增长潜力与发展机会,分析东南亚市场竞争态势、销售模式、客户偏好、整体市场营商环境,为国内企业出海开展业务提供客观参考意见。 本文核心内容: 市场空间:全球行业市场空间、东南亚市场发展空间。 竞争态势:全球份额,东南亚市场企业份额。 销售模式:东南亚市场销售模式、本地代理商 客户情况:东南亚本地客户及偏好分析 营商环境:东南亚营商环境分析 本文纳入的企业包括国外及印尼本土企业,以及相关上下游企业等,部分名单 QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。邮箱:market@qyresearch.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值