PAT 1006

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

一、试题分析: 

  • 题目要求:求出第一个到达开门的学生id、最后一个离开关门的学生id
  • 重点在于:
  1. 如何比较时间的大小:字符串 转化为 具体多少秒
  2. 如何求得最早和最晚学生的id:根据下标一样,不断更新最早和最晚学生的id
  • 注意INT_MAX需要引入#include <climits>,否则pat编译错误
#include <iostream>
#include <climits>
using namespace std;

int main()
{
    int m;
    cin>>m;

    string id[m]; //存储学生编号
    string time[m][2]; //存储学生进入和离开的时间
    for(int i=0;i<m;i++){
        cin>>id[i];
        cin>>time[i][0]>>time[i][1];
    }

    int early;
    string strearly;
    int minearly=INT_MAX; //初始化最早时间为最大
    int late;
    int maxlate=0; //初始化最晚时间为最小
    string strlate;
    for(int i=0;i<m;i++){
        //将字符串进入时间转化为秒数
        early=(time[i][0][6]-'0')*10+(time[i][0][7]-'0')+((time[i][0][3]-'0')*10+(time[i][0][4]-'0'))*60+((time[i][0][0]-'0')*10+(time[i][0][1]-'0'))*60*60;
        if(early<minearly){
            minearly=early;
            strearly=id[i]; //更新最早到达的学生id
        }
        //将字符串离开时间转化为秒数
        late=(time[i][1][6]-'0')*10+(time[i][1][7]-'0')+((time[i][1][3]-'0')*10+(time[i][1][4]-'0'))*60+((time[i][1][0]-'0')*10+(time[i][1][1]-'0'))*60*60;
        if(late>maxlate){
            maxlate=late;
            strlate=id[i]; //更新最晚离开的学生id
        }
    }
    cout<<strearly<<' '<<strlate;
    return 0;
}
发布了28 篇原创文章 · 获赞 2 · 访问量 6565

猜你喜欢

转载自blog.csdn.net/Ariel_x/article/details/104023822