美文网首页
2020-03-07

2020-03-07

作者: joker_luo | 来源:发表于2020-03-07 21:14 被阅读0次

    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`
    
    //本体思路比较简单,就是找谁最早来,谁最晚离开,给出的时间段是来的时间和离开的时间
    //来的时间可以当成从零点0分0秒开始,到来的时间的时间段,全部换算成秒,
    //来的最早的一定是来的时间总秒数最小的;离开最晚的即离开时间总秒数最大的。 
    #include<iostream>
    #include<string>
    using namespace std;
    typedef struct Data{
        string name;
        int beginhour;
        int beginminute;
        int beginsecond;
        int endhour;
        int endmintue;
        int endsecond;
    }Data;
    int main(){
        Data arr[500];
        int M;
        cin>>M;
        for(int i=0;i<M;++i){
            cin>>arr[i].name;
            scanf("%d:%d:%d",&arr[i].beginhour,&arr[i].beginminute,&arr[i].beginsecond);
            scanf("%d:%d:%d",&arr[i].endhour,&arr[i].endmintue,&arr[i].endsecond);
        }
        //为了进入循环,使unlock的初值为第一个人到的总秒数,方便后续比较,设置一个大于一天的总秒数即可。 
        int unlock=99999999;
        //与上面一样,方便进入循环比较,第一个人离开的总秒数,方便比较,小于0即可。 
        int lock = -1;
        //用于标记i,方便输出名字。  
        int n=0,m=0;
        int data=0;
        int data2=0;
        for(int j=0;j<M;++j){
            data = arr[j].beginhour*3600+arr[j].beginminute*60+arr[j].beginsecond;
            data2 = arr[j].endhour*3600+arr[j].endmintue*60+arr[j].endsecond;
            if(unlock>data){
                unlock = data;
                n = j;
            }
            if(lock<data2){
                lock = data2;
                m = j;
            }
        }
        cout<<arr[n].name<<" "<<arr[m].name;
        return 0;
    }
    

    PS:我没学过算法,都是想到怎么做就怎么做,没考虑复杂度的问题,等以后有时间了再去学习最优解吧。

    相关文章

      网友评论

          本文标题:2020-03-07

          本文链接:https://www.haomeiwen.com/subject/iqqkdhtx.html