PAT甲级——1006 Sign In and Sign Out (25分)

思路:

将每个同学进入和离开的时间转换成秒为单位方便比较;

找出进入时间最早的和离开时间最晚的;

我用set去搞,最后再遍历一遍找下标对应输出名字,这样复杂度应该算是比较低;

当然这题数据量不大,考虑复杂度也没有过多的意义……

1006 Sign In and Sign Out (25分)

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:

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
//1006
/*
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <set>
using namespace std;

struct P{
    string name;
    string t1;
    string t2;
};

int main(){

    int m;
    cin>>m;
    P p[500];
    int T1[500],T2[500];
    set<int> a1,a2;
    int b1,b2;//记录最早时间和最晚时间的下标
    for(int i=0;i<m;i++){
        cin>>p[i].name>>p[i].t1>>p[i].t2;
        T1[i]=(p[i].t1[0]-'0')*10*3600+(p[i].t1[1]-'0')*3600+(p[i].t1[3]-'0')*10*60+(p[i].t1[4]-'0')*60+(p[i].t1[6]-'0')*10+(p[i].t1[7]-'0');
        a1.insert(T1[i]);
        T2[i]=(p[i].t2[0]-'0')*10*3600+(p[i].t2[1]-'0')*3600+(p[i].t2[3]-'0')*10*60+(p[i].t2[4]-'0')*60+(p[i].t2[6]-'0')*10+(p[i].t2[7]-'0');
        a2.insert(T2[i]);
    }
    for(int i=0;i<m;i++){
        if(T1[i]==*(a1.begin())){
            b1=i;
        }
        if(T2[i]==*(a2.rbegin())){
            b2=i;
        }
    }
    cout<<p[b1].name<<" "<<p[b2].name<<endl;

    return 0;
}
发布了298 篇原创文章 · 获赞 301 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/qq_41895747/article/details/104353817
今日推荐