PAT - 甲级 - 1006. Sign In and Sign Out (25

来源:互联网 发布:会计网络培训 编辑:程序博客网 时间:2024/05/16 14:27

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 Specification:

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.

Output Specification:

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.

Sample Input:
3CS301111 15:30:28 17:00:10SC3021234 08:00:00 11:25:25CS301133 21:45:00 21:58:40
Sample Output:
SC3021234 CS301133

题目大概意思是先输入一个整数n表示人数,接下来n行每行依此输出人名  到达时间 离开时间。找出到达时间最早的人和离开最晚的人输出这两个人名 中间一个空格。

#include<iostream>#include<cstdio>#include<string>#include<algorithm>using namespace std;struct person{string name;int hour1;int minute1;int second1;int hour2;int minute2;int second2;};bool cmp1(person p1,person p2){if(p1.hour1!=p2.hour1){return p1.hour1<p2.hour1;}else if(p1.minute1!=p2.minute1){return p1.minute1<p2.minute1;}else{return p1.second1<p2.second1;}}bool cmp2(person p1,person p2){if(p1.hour2!=p2.hour2){return p1.hour2<p2.hour2;}else if(p1.minute2!=p2.minute2){return p1.minute2<p2.minute2;}else{return p1.second2<p2.second2;}}int main(){int n;cin>>n;person p[n];for(int i=0 ;i<n ;i++){cin>>p[i].name;scanf("%d:%d:%d%d:%d:%d",&p[i].hour1,&p[i].minute1,&p[i].second1,&p[i].hour2,&p[i].minute2,&p[i].second2);}sort(p,p+n,cmp1);//寻找最早到达的人 cout<<p[0].name<<" ";sort(p,p+n,cmp2);//寻找最晚离开的人 cout<<p[n-1].name<<endl;return 0;}






0 0