1006 Sign In and Sign Out(25 分)

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

//input:         M 个学生
//      每个学生的:ID_number Sign_in_time Sign_out_time
//output: 先进来的学生ID_number  后进来的学生ID_number
//注意:每个人的进来时间和出去时间是不一样的,时间格式为HH:MM:SS,ID_number<=15 characters

//都是字符串,也可以直接比较大小。
#include<stdio.h>
#include<iostream>
#include<string>

using namespace std;

int charToInt(char ch)
{
    int rs = 0;
    switch(ch)
    {
    case '0':
        rs = 0;
        break;
    case '1':
        rs = 1;
        break;
    case '2':
        rs = 2;
        break;
    case '3':
        rs = 3;
        break;
    case '4':
        rs = 4;
        break;
    case '5':
        rs = 5;
        break;
    case '6':
        rs = 6;
        break;
    case '7':
        rs = 7;
        break;
    case '8':
        rs = 8;
        break;
    case '9':
        rs = 9;
        break;
    }
    return rs;
}

int changTime(string s) //时间用秒表示
{
    int h = s[0]*10 + s[1];
    int m = s[3]*10 + s[4];
    int S = s[6]*10 + s[7];

    return (h*3600 + m*60 + S);
}

int main()
{
    //输入参数
    int M;
    cin >> M;

    string ID_number[100];
    int Sign_in_time[100]; //用秒表示的时间
    int Sign_out_time[100];

    string sIn,sOut;//输入时间
    for(int i = 0;i < M;i++)
    {
        cin >> ID_number[i];
        cin >> sIn;
        Sign_in_time[i] = changTime(sIn);
        cin >> sOut;
        Sign_out_time[i] = changTime(sOut);
    }

    int minInTime = Sign_in_time[0];
    int maxOutTime = Sign_out_time[0];

    string firstInID,lastOutID;
    for(int i = 0;i < M;i++)
    {
        if(Sign_in_time[i] <= minInTime)  //此处的等于号一定要,因为第一个ID也需要赋值
        {
            minInTime = Sign_in_time[i];
            firstInID = ID_number[i];
        }
        if(Sign_out_time[i] >= maxOutTime)
        {
            maxOutTime = Sign_out_time[i];
            lastOutID = ID_number[i];
        }
    }

    cout << firstInID << " " << lastOutID<<endl;


    return 0;
}
原文地址:https://www.cnblogs.com/meiqin970126/p/9599996.html