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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ASJBFJSB/article/details/89054774

PAT 1141.PAT Ranking of Institutions (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.

输入格式:
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.

输出格式:
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.

输入样例:

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

输出样例:

SC3021234 CS301133

题目分析:sort的使用,求解最早签到时间和最晚签到离开时间。主要的难度在于签到和签到离开时间的处理,当成字符串处理会比较直接简单。

注意:由于题目可以转化为输出最早签到的人名和最晚签到离开的人,此题也也可以直接在输入时间比较。在一天中按照hh:mm:ss的形式,最早为00:00:00,最晚为23:59:59

AC代码1:

#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

struct people{
    string id, sign_in, sign_out;
    people(string id, string sign_in, string sign_out){
        this->id = id;
        this->sign_in = sign_in;
        this->sign_out = sign_out;
    }
};

vector<people> v;

bool cmpin(people a, people b) {return a.sign_in < b.sign_in;}
bool cmpout(people a, people b){return a.sign_out > b.sign_out;}
void print(people a){
    cout<<a.id<<" "<<a.sign_in<<" "<<a.sign_out<<endl;
} 

int main(){
    //freopen("in.txt", "r", stdin);
    int n;
    scanf("%d", &n);
    string id,sign_in,sign_out;
    for(int i=0; i<n; ++i){
        cin>>id>>sign_in>>sign_out;
        v.push_back(people(id, sign_in, sign_out));
    }
    //for_each(v.begin(), v.end(), print);
    sort(v.begin(), v.end(), cmpin);
    cout<<v[0].id<<" ";
    sort(v.begin(), v.end(), cmpout);
    cout<<v[0].id<<endl;
    return 0;
}

AC代码2:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string earliest = "23:59:59";
    string latest  = "00:00:00";
    string earliestName, latestName;
    int n;
    scanf("%d", &n);
    string id, early, finally;
    for(int i=0; i<n; ++i){
        cin>>id>>early>>finally;
        if(early <= earliest){
            earliest = early;
            earliestName = id;
        }
        if(finally >= latest){
            latest = finally;
            latestName = id;
        }
    }
    cout<<earliestName<<" "<<latestName<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ASJBFJSB/article/details/89054774