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

题目链接:

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

 

题目大意:

给出一日中进出机房人员的信息,要求你给出机房开关门人的ID编号。进出人员的信息格式:ID编号 进入时间 出去时间。时间以时:分:秒格式给出,24小时计时,保证所有人的信息都是有效的,即进入时间早于出去时间。

 

思路分析:

比较容易的理解是全部读入,然后排序之后去最值,复杂度较大,算法笔记中说可以过,但是我尝试了对全部时间排序,和分进出时间排序,都没有全部通过,大家可以试试。

全部时间排序:

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

using namespace std;

struct Pnode{
    char id[20];
    int h, m, s;
};

bool great(Pnode node1, Pnode node2){
    if(node1.h != node2.h) return node1.h > node2.h;
    if(node1.m != node2.m) return node1.m > node2.m;
    return node1.s > node2.s;
}

int main(){
    int n;
    scanf( "%d", &n) ;
    vector<Pnode> ans(n);
    for(int i = 0; i < n; i++){
        Pnode temp;
        scanf( "%s %d:%d:%d", temp.id, &temp.h, &temp.m, &temp.s); ans.push_back(temp);
        //if(great(ans2, temp)) ans2=temp;   //将最早的时间令为ans2.
        scanf(" %d:%d:%d",&temp.h, &temp.m, &temp.s); ans.push_back(temp);

        //if(great(temp, ans1)) ans1=temp;   //将最迟的时间令为ans1.
    }
    sort(ans.begin(), ans.end(), great);
    printf("%s %s", ans[2*n-1].id ,ans[0].id);
    return 0;
}

 

 

分进出时间排序:

#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

struct Pnode{
    char id[20];
    int h, m, s;
};

bool great(Pnode node1, Pnode node2){
    if(node1.h != node2.h) return node1.h > node2.h;
    if(node1.m != node2.m) return node1.m > node2.m;
    return node1.s > node2.s;
}

int main(){
    int n;
    scanf( "%d", &n) ;
    vector<Pnode> ans1(n);
    vector<Pnode> ans2(n);
    for(int i = 0; i < n; i++){
        Pnode temp;
        scanf( "%s %d:%d:%d", temp.id, &temp.h, &temp.m, &temp.s); ans1.push_back(temp);
        //if(great(ans2, temp)) ans2=temp;   //将最早的时间令为ans2.
        scanf(" %d:%d:%d",&temp.h, &temp.m, &temp.s); ans2.push_back(temp);

        //if(great(temp, ans1)) ans1=temp;   //将最迟的时间令为ans1.
    }
    sort(ans1.begin(), ans1.end(), great);
    sort(ans2.begin(), ans2.end(), great);
    printf("%s %s", ans1[n-1].id ,ans2[0].id);
    return 0;
}

在参考算法笔记的解析后,可以得出更优的算法,即在输入的时候即得出最早时间和最迟时间。分别用ans1,ans2记录。

#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){
    if(node1.h != node2.h) return node1.h > node2.h;
    if(node1.m != node2.m) return node1.m > node2.m;
    return node1.s > node2.s;
}

int main(){
    int n;
    scanf( "%d", &n) ;
    ans1.h = 0, ans1.m = 0, ans1.s = 0;
    ans2.h = 24, ans2.m = 60, ans2.s = 60;
    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;   //将最早的时间令为ans2.
        scanf(" %d:%d:%d",&temp.h, &temp.m, &temp.s);
        if(great(temp, ans1)) ans1=temp;   //将最迟的时间令为ans1.
    }
    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、付费专栏及课程。

余额充值