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


解题思路

用两个Date对象(startOne、endOne)承载最早开启时间和最晚离开时间,每次输入一条数据就与这两个时间对象进行比较

提交代码

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;/*** created by chen* on 2017/9/13 23:37*/
public class Main{public static void main(String[] args) {Scanner sc = new Scanner(System.in);//保存开门和关门人的idString openId = null, closeId = null;//保存最早开启时间和最晚关闭时间Date startOne = null;Date endOne = null;//创建日期转换对象HH:mm:ss为时分秒,年月日为yyyy-MM-ddDateFormat df = new SimpleDateFormat("HH:mm:ss");int N = Integer.parseInt(sc.nextLine());for (int i = 0; i < N; i++) {String str = sc.nextLine();String[] messages = str.split(" ");String id = messages[0];String start = messages[1];String end = messages[2];//第一次时两个时间对象为空时,将该记录作为首次参考记录if(startOne == null){try {startOne = df.parse(start);} catch (ParseException e) {e.printStackTrace();}openId = id;}if(endOne == null){try {endOne = df.parse(end);} catch (ParseException e) {e.printStackTrace();}closeId = id;}try {Date dt1 = df.parse(start);Date dt2 = df.parse(end);//时间比最早开启时间早if(dt1.getTime() < startOne.getTime()){startOne = dt1;openId = id;}//时间比最晚关门时间晚if(dt2.getTime() > endOne.getTime()){endOne = dt2;closeId = id;}} catch (ParseException e) {e.printStackTrace();}}System.out.println(openId + " " + closeId);}
}

参考文章

java中的日期比较大小

1006. Sign In and Sign Out (25)相关推荐

  1. [Java] 1006. Sign In and Sign Out (25)-PAT甲级

    1006. Sign In and Sign Out (25) At the beginning of every day, the first person who signs in the com ...

  2. 【PAT甲】1006 Sign In and Sign Out (25分)循环模拟

    problem 1006 Sign In and Sign Out (25分) At the beginning of every day, the first person who signs in ...

  3. PAT甲级1006 Sign In and Sign Out:[C++题解]字符串处理

    文章目录 题目分析 题目链接 题目分析 读入三个字符串. 需要记录最早开门的人的id和时间,最晚离开的人的id和时间. 本题的简单之处在于 时间的位数都相同,比如 10点和01点.因此这样的时间大小比 ...

  4. Sign In and Sign Out

    1006. Sign In and Sign Out (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  5. [sign in和sign up哪个是注册?哪个是登入?]

    sign in和sign up的意思: 如果是网站的话sign up是注册,sign in是登录的意思,另外,sign out退出

  6. (翻译)‘Sign Up’ 和‘Sign In’按钮让用户混淆的原因

      你能多快看出"Sign Up(PS:注册)"和"Sign In(PS:登录)"的区别?将它们作为按钮名称同时使用,会让用户点错按钮.出现上述问题并不是用户的 ...

  7. 【PAT - 甲级1006】Sign In and Sign Out (25分)(STLmap)

    题干: At the beginning of every day, the first person who signs in the computer room will unlock the d ...

  8. 1006. Sign In and Sign Out (25)-PAT甲级真题

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  9. PAT(A)1006 Sign In and Sign Out (25 分)

    Input Specification: Output Specification: Sample Input: 3 CS301111 15:30:28 17:00:10 SC3021234 08:0 ...

最新文章

  1. 网络对抗技术-实验报告一
  2. javascript学习系列(3):数组中的foreach方法
  3. Mybatis解决jdbc编程的问题以及mybatis与hibernate的不同
  4. Ubuntu,kubuntu与xubuntu的差别 Ubuntu各版本主要差异
  5. 操作系统内存管理、Cache调度策略学习
  6. python中format函数用法简书_Python format 格式化函数
  7. 微信蓝牙协议一:协议文档查阅方法和空中数据解析示例
  8. windows驱动开发技术详解 VC6与DDK搭配使用的设置
  9. bochs运行xp_bochs xp镜像下载-bochs xp镜像下载 v2.5.1精简版-下载啦
  10. 关于数据库求候选键问题
  11. ORA-20011 问题处理
  12. android 模拟器后缀名,apk是什么文件?apk文件模拟器是什么?
  13. Web服务http日志收集
  14. 空想科学教程 (爆笑)1
  15. 【漫画程序员涛哥】程序员是如何换灯泡的?
  16. 【C语言】杨辉三角(数组)
  17. iOS-中集成百度echarts3-0
  18. web前端大作业--黑色电影资讯博客网页设计(电影主题-HTML+CSS+JavaScript)
  19. 分析OpenSL回声Demo
  20. 传值和传引用(米斯特吴22)

热门文章

  1. 代理模式中的静态代理
  2. Spring-Cloud中的负载
  3. Shiro结合redis的统一会话管理:自定义会话管理器
  4. Eurek Ribbon Feign常见问题及解决
  5. ThreadLocal - Java多线程编程
  6. 方法重载(overload)和方法重写(override)的比较
  7. python原生字符串可以参与比较_正则表达式中对于原生字符串的理解
  8. python中output使用_python-02.输入Input/输出Output
  9. autoflowchart软件使用步骤_编程怎么入门,7个步骤带你飞, 网友:上车!
  10. 200801一阶段1函数封装