1006 Sign In and Sign Out (25 point(s))

1006 Sign In and Sign Out (25 point(s))

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

题目大意:

1.给输入数量M

2.然后对应的M组数据,每组数据对应 id  , 进入时间 , 出去时间  (保证一个时刻只有一个人进或者出,且进入时间一定比出去时间要早)

3.找到最早的时间的id ,再找到最晚出去的时间的id 并输出,

解决思路

1.创建结构体,放入id,开始时间,结束时间,都用string

2.定义比较函数,因为通过时间格式可以看出,实质上还是字符串之间的大小的比较 所以直接str进行比较就行了

3.对应输出即可

注意:

1.用strcmp 函数进行比较的话,会输出符号,但是符号是会被认为非0 的,所以要进行判断

可以参考如下代码块


bool camp1(const node1 &one,const node1 &two ){
    int cm = strcmp(one.start.c_str(),two.start.c_str());
    return cm<0;

}


bool camp2(const node2 &one ,const node2 &two ){
    int cm = strcmp(one.end.c_str(),two.end.c_str());
    return cm>0;
}

camp1 代表要递增的方式进行排序,可以想象cm<0时返回true ,即排在前面

camp2 就相反了

2.用sort方法注意格式:

 sort(node1s.begin(),node1s.end(),camp1);
 sort(node2s.begin(),node2s.end(),camp2);

代码:

#include<string>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;

struct node1{
    string id;
    string start;

};
struct node2{
    string id;
    string end;

};


bool camp1(const node1 &one,const node1 &two ){
    int cm = strcmp(one.start.c_str(),two.start.c_str());
    return cm<0;

}


bool camp2(const node2 &one ,const node2 &two ){
    int cm = strcmp(one.end.c_str(),two.end.c_str());
    return cm>0;
}

int main(){

    vector<node1> node1s;
    vector<node2> node2s;
    int M=0;
    cin>>M;
    for(int i=0;i<M;i++){
        string id_;
        string start_;
        string end_;
        cin>>id_>>start_>>end_;
        node1 temp1;
        temp1.id = id_;
        temp1.start = start_;
        node2 temp2;
        temp2.id = id_;
        temp2.end = end_;
        node1s.push_back(temp1);
        node2s.push_back(temp2);
    }

    sort(node1s.begin(),node1s.end(),camp1);
    sort(node2s.begin(),node2s.end(),camp2);

    cout<<node1s[0].id<<" "<<node2s[0].id;

    return 0;

}

词汇:

consistent

adj. 始终如一的,一致的;坚持的

猜你喜欢

转载自blog.csdn.net/Willen_/article/details/83998929