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中的日期比较大小

原创粉丝点击