pat 甲级 A1006 Sign In and Sign Out (25分)

题目链接:

https://pintia.cn/problem-sets/994805342720868352/problems/994805516654460928

 

 

题目分析:

根据一天中多人到达机房时间和离开时间,给出开门以及锁门人的ID。显然可以对到达时间,以及离开时间进行排序,最早的时间即为第一个人到达时间,最晚时间即为最后一个人离开时间。但是这容易超时。转换思路,可以想到因为只需知道最早到达时间和最晚离开时间,那么在输入的时候就可以对其进行筛选。

 

参考代码:

#include <iostream>
#include <cstdio>
using namespace std;
struct Pnode{
    char id[20];
    int h, m, s;
}ans1, ans2, temp;
bool great(Pnode node1, Pnode node2){
    return a.h*3600 + a.m*60 + a.s > b.h*3600 + b.m*60 + b.s;
}
int main(){
    int n;
    scanf( "%d", &n) ;
    ans1.h = 0, ans1.m = 0, ans1.s = 0;     //将最迟的时间令为ans1,初始设置值为0点
    ans2.h = 24, ans2.m = 60, ans2.s = 60;  //将最早的时间令为ans2,初始设置值为24点
    for(int i = 0; i < n; i++){
        scanf( "%s %d:%d:%d", temp.id, &temp.h, &temp.m, &temp.s);
        if(great(ans2, temp)) ans2=temp;   
        scanf(" %d:%d:%d",&temp.h, &temp.m, &temp.s);
        if(great(temp, ans1)) ans1=temp;   
    }
    printf("%s %s", ans2.id ,ans1.id);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值