1006. Sign In and Sign Out (25)
来源:互联网 发布:一年java专业技能描述 编辑:程序博客网 时间:2024/05/16 15:50
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); //保存开门和关门人的id String openId = null, closeId = null; //保存最早开启时间和最晚关闭时间 Date startOne = null; Date endOne = null; //创建日期转换对象HH:mm:ss为时分秒,年月日为yyyy-MM-dd DateFormat 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)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- 1006. Sign In and Sign Out (25)
- HDU
- win32汇编无头文件编译成功
- 程序32
- 牛客网-剑指offer-20-包含min函数的栈
- Ubuntu 下实现FTP服务器,SSH服务器,邮件服务器,Web服务器(一)
- 1006. Sign In and Sign Out (25)
- 程序33
- rem布局在webview中页面错乱
- Synchronized与Lock锁的区别
- bzoj 1681: [Usaco2005 Mar]Checking an Alibi 不在场的证明(BFS)
- Spring 框架简介
- H5中关于自定义属性的设置和获取
- Java入门之编程基础(二)
- 程序34