pat-1006 Sign In and Sign Out (25)(多条件排序 + 结构体 简单题)

题目链接

思路:

就是把两个时间in,out排一下序,找出in 中最小的,和out中最大的时间,显然 结构体 + 对cmp动手脚。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
typedef struct student{
    char snum[20];
    int in_h,out_h;
    int in_m,out_m;
    int in_s,out_s;
};
bool cmp_in(student a,student b)//对进门时间排序,小时一样就根据分来排,分一样根据秒来排
{
    if(a.in_h < b.in_h)
        return true;
    else if(a.in_h == b.in_h)
    {
        if(a.in_m < b.in_m)
            return true;
        else if(a.in_m == b.in_m)
        {
            if(a.in_s < b.in_s)
                return true;
        }
    }
    return false;
}
bool cmp_out(student a,student b)//对出门时间排序,就是把上面的in换成out就行
{
    if(a.out_h < b.out_h)
        return true;
    else if(a.out_h == b.out_h)
    {
        if(a.out_m < b.out_m)
            return true;
        else if(a.out_m == b.out_m)
        {
            if(a.out_s < b.out_s)
                return true;
        }
    }
    return false;
}

int main()
{
    int m;
    scanf("%d",&m);
    student stu[105];
    for(int i = 0;i < m ;i ++)
    {
        scanf("%s %d:%d:%d %d:%d:%d",&stu[i].snum,&stu[i].in_h,&stu[i].in_m,&stu[i].in_s,&stu[i].out_h,&stu[i].out_m,&stu[i].out_s);
    }
    sort(stu,stu + m,cmp_in);
    printf("%s ",stu[0].snum);
    sort(stu,stu + m,cmp_out);
    printf("%s",stu[m - 1].snum);
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzyhfxt/article/details/81736450