【PAT】1006. Sign In and Sign Out (25)【结构体+排序函数cmp】

题目描述

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 FORMAT

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.

翻译:每个输入文件包括一组测试数据。每组测试数据包含那天的记录。测试数据第一行是一个正整数M,为记录的总条数,接下来的M行,都是以下输入格式:

ID_number Sign_in_time Sign_out_time
提供的时间格式为HH:MM:SS,并且ID号为一串不超过15个字符的字符串。

OUTPUT FORMAT

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.

翻译:对于每组测试数据,输出一行,包括当天开门的人的ID和关门的人的ID。这两个ID号中间必须用一个空格隔开。注意:数据保证记录是不矛盾的,这说明每个人到机房的时间一定小于离开机房的试卷,并且每个人都不在同一时间到达或离开。


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


解题思路

先建立结构体保存数据,然后再编写两个排序函数即可。时间比较可以之间用string的>和<来判断,而且题目保证所有人的时间都不相同,就可以直接写出来了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#define INF 99999999
using namespace std;
int N;
struct People{
    string name,inTime,outTime;
    People(string b,string i,string o):name(b),inTime(i),outTime(o){}
};
bool cmp1(People a,People b){
    return a.inTime<b.inTime;
}
bool cmp2(People a,People b){
    return a.outTime>b.outTime;
}
vector<People> p;
int main(){
    scanf("%d",&N);
    string a,b,c;
    for(int i=0;i<N;i++){
        cin>>a>>b>>c;
        p.push_back(People(a,b,c));
    }
    sort(p.begin(),p.end(),cmp1);
    cout<<p[0].name<<" ";
    sort(p.begin(),p.end(),cmp2);
    cout<<p[0].name<<endl;
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值