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

解题思路: 文章大意是,记录每个人的名字,以及进入门,以及出门的时间,要求找到开门和锁门的人。

众所周知,开门的肯定是所有时间里面最早进入的人,关门的肯定就是所有时间里面最晚出来的人,所有这两个是不相关的两件事。因为一个人的信息有多种类型的数据,所以不妨设置一个结构体,一个结构体结点在本题相当于一条信息(名字--string,小时--int,分钟--int,秒--int),同时因为时间有进入与出去,不妨设置数组:

struct people
{
    string name;
    int hour[2];//[0]表示进入,[1]表示离开
    int minute[2];//[0]表示进入,[1]表示离开
    int second[2];//[0]表示进入,[1]表示离开

};

 存好后,依次比较小时、分钟、秒的数字大小,顺序为 如果hour已经小了,就没必要比较分钟了,直接把名字拷过来,在小时相等的情况下比较分钟的大小,以此类推:

 源代码:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<string.h>
  4. using namespace std;
  5. struct people
  6. {
  7.     string name;
  8.     int hour[2];//[0]表示进入,[1]表示离开
  9.     int minute[2];
  10.     int second[2];
  11. };
  12. int main()
  13. {
  14.     int n;
  15.     int min_hour=100,min_minute=100,min_second=100;
  16.     string min_name;
  17.     int max_hour=0,max_minute=0,max_second=0;
  18.     string max_name;
  19.     cin>>n;
  20.     people peo[15];
  21.     for(int i=0;i<n;i++)
  22.     {
  23.         cin>>peo[i].name;
  24.         scanf("%d:%d:%d",&peo[i].hour[0],&peo[i].minute[0],&peo[i].second[0]);
  25.         scanf("%d:%d:%d",&peo[i].hour[1],&peo[i].minute[1],&peo[i].second[1]);
  26.     }
  27.     for(int i=0;i<n;i++)
  28.     {
  29.         if(peo[i].hour[0]<min_hour)
  30.         {
  31.             min_hour=peo[i].hour[0];
  32.             min_name=peo[i].name;
  33.         }
  34.         else if(peo[i].hour[0]==min_hour)
  35.         {
  36.             if(peo[i].minute[0]<min_minute)
  37.             {
  38.                 min_minute=peo[i].minute[0];
  39.                 min_name=peo[i].name;
  40.             }
  41.             else if(peo[i].minute[0]==min_minute)
  42.             {
  43.                 if(peo[i].second[0]<min_second)
  44.                 {
  45.                     min_second=peo[i].second[0];
  46.                     min_name=peo[i].name;
  47.                 }
  48.                 else continue;
  49.             }
  50.         }
  51.         //printf("%d:%d:%d",peo[i].hour[0],peo[i].minute[0],peo[i].second[0]);
  52.         //printf("%d:%d:%d",peo[i].hour[1],peo[i].minute[1],peo[i].second[1]);
  53.         if(peo[i].hour[1]>max_hour)
  54.         {
  55.             max_hour=peo[i].hour[1];
  56.             max_name=peo[i].name;
  57.         }
  58.         else if(peo[i].hour[1]==max_hour)
  59.         {
  60.             if(peo[i].minute[1]>max_minute)
  61.             {
  62.                 max_minute=peo[i].minute[1];
  63.                 max_name=peo[i].name;
  64.             }
  65.             else if(peo[i].minute[1]==max_minute)
  66.             {
  67.                 if(peo[i].second[1]>max_second)
  68.                 {
  69.                     max_second=peo[i].second[0];
  70.                     max_name=peo[i].name;
  71.                 }
  72.                 else continue;
  73.             }
  74.         }
  75.     }
  76.     cout<<min_name<<" "<<max_name<<endl;
  77. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值