前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT(甲级)1006.Sign In and Sign Out(25)

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

作者头像
lexingsen
发布2022-02-25 08:02:34
2210
发布2022-02-25 08:02:34
举报
文章被收录于专栏:乐行僧的博客

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.

输入样例:

代码语言:javascript
复制
3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

输出样例:

代码语言:javascript
复制
SC3021234 CS301133

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

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

AC代码1:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档