[PAT A1006]Sign In and Sign Out

[PAT A1006]Sign In and Sign Out

题目描述

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.

输入格式

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.

输入样例

3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

输出样例

SC3021234 CS301133

解析

  1. 这道题目实则不难,但是我的代码却写的很长,这很浪费时间,所以我会对比我的代码和柳神的代码,来找到自己的不足并加以改进
  2. 题目的大意很好理解,这题翻译并不难,第一行输入一个M,表示的是记录了公司M个人进入公司和出公司的时间,接着M行,输出的是M个人的ID和出入记录,输出结果是每天第一个进公司的人和最后一个出公司的人的ID
  3. 我的代码如下
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct person
{
 string ID;  //员工的ID
 int in_time[3];  //员工进入的时间
 int out_time[3];  //员工出去的时间
};
bool cmp1(person p1, person p2) //cmp1用于对进入时间按升序排序
{
 if (p1.in_time[0] == p2.in_time[0])
 {
  if (p1.in_time[1] == p2.in_time[1])
  {
   return p1.in_time[2] < p2.in_time[2];
  }
  else return p1.in_time[1] < p1.in_time[1];
 }
 else return p1.in_time[0] < p2.in_time[0];
}
bool cmp2(person p1, person p2)  //cmp2用于对离开时间的按降序排序
{
 if (p1.out_time[0] == p2.out_time[0])
 {
  if (p1.out_time[1] == p2.out_time[1])
  {
   return p1.out_time[2] > p2.out_time[2];
  }
  else return p1.out_time[1] > p1.out_time[1];
 }
 else return p1.out_time[0] > p2.out_time[0];
}
int main()
{
 int M;
 cin >> M;
 vector<person> record;   //定义一个vector
 for (int i = 0; i < M; i++)
 {
  person t;
  cin >> t.ID;
  scanf_s("%d:%d:%d", &t.in_time[0], &t.in_time[1], &t.in_time[2]);
  scanf_s("%d:%d:%d", &t.out_time[0], &t.out_time[1], &t.out_time[2]);
  record.push_back(t);
 }
 sort(record.begin(), record.end(), cmp1);
 cout << record[0].ID << " ";
 sort(record.begin(), record.end(), cmp2);
 cout << record[0].ID;
 return 0;
}
  1. 我觉得我最大的问题就是太为了模拟而模拟,比如本题有一个人结构,我就建立一个结构体,并且将他们的进出时间都适用数据结构存储起来了,并且将它们实体化,通过重构cmp函数排序,最后输出结果。
  2. 我觉得对于做题简化来说,一定要认真看能否通过一遍过程解题?能不能不使用结构体变量?这里柳神的代码非常值得我学习,因为她极懒(开玩笑^ - ^),能省的步骤都使用非常巧妙的办法解决了,例如很多题目她在输入的过程中不断解题,而不是输入完之后解题
  3. 要点分析:
    a.首先使用minn装填最早的进入时间,maxn装填最迟离开时间
    b.使用string t存储每一次输入过程中的ID,然后输入时间
    c.这里使用了一个技巧,就是把时间换成秒再比较大小,这个十分值得学习
    d.通过这种方法,就完全没必要建立结构体和结构体数组,代码精简
这里引用了柳神的代码,懒得自己再写了qvq
#include <iostream>
#include <climits>
using namespace std;
int main() {
int n, minn = INT_MAX, maxn = INT_MIN;
scanf("%d", &n);
string unlocked, locked;
for(int i = 0; i < n; i++) {
string t;
cin >> t;
int h1, m1, s1, h2, m2, s2;
scanf("%d:%d:%d %d:%d:%d", &h1, &m1, &s1, &h2, &m2, &s2);
int tempIn = h1 * 3600 + m1 * 60 + s1;
int tempOut = h2 * 3600 + m2 * 60 + s2;
if (tempIn < minn) {
minn = tempIn;
unlocked = t;
}
if (tempOut > maxn) {
maxn = tempOut;
locked = t;
}
}
cout << unlocked << " " << locked;
return 0;
}
水平有限,如果代码有任何问题或者有不明白的地方,欢迎在留言区评论;也欢迎各位提出宝贵的意见!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值