【结构体排序】HDU-1234 开门人和关门人

在这里插入图片描述
在这里插入图片描述

注解

1、结构体排序。
2、字符串的输入要注意,以及scanf如何快速方便的输入带格式数据(如本题的时间数据)。

代码

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

const int LEN = 20;

struct Peo {
    char id[LEN];
    int start;
    int end;
};

int compare(Peo a, Peo b) {
    return a.start<b.start;
}

int compare2(Peo a, Peo b) {
    return a.end>b.end;
}

int main() {

    int N;
    scanf("%d", &N);

    for(int i=0; i<N; i++) {
        int M;
        scanf("%d", &M);
        Peo peo[M];
        for(int j=0; j<M; j++) {
            scanf("%s", peo[j].id);
            int a, b, c;
            scanf("%d:%d:%d", &a, &b, &c);
            int start = a*3600+b*60+c;
            scanf("%d:%d:%d", &a, &b, &c);
            int end = a*3600+b*60+c;
            peo[j].start = start;
            peo[j].end = end;
        }
        sort(peo, peo+M, compare);
        char ans1[LEN];
        memcpy(ans1, peo[0].id, strlen(peo[0].id)+1);
        sort(peo, peo+M, compare2);
        char ans2[LEN];
        memcpy(ans2, peo[0].id, strlen(peo[0].id)+1);
        printf("%s %s\n", ans1, ans2);
    }

    return 0;
}

结果

在这里插入图片描述

发布了475 篇原创文章 · 获赞 19 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/zhanggirlzhangboy/article/details/103717686