【PAT甲级】1006 Sign In and Sign Out(25)——JAVA实现

来源:互联网 发布:淘宝怎么优化 编辑:程序博客网 时间:2024/06/06 05:36

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

题目大意
给定一个数M,表示有M条记录,每条记录包括某人的ID以及他的登录、登出时间三项,找出最早登录的人以及最迟登出的人打印他们的ID。

import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.Scanner;//根据题目要求定义一个类,里面包含三种需要比较的数据。class Employee {    String name;    String in_time;    String out_time;}//定义一个比较器,并设置一个标识来确定比较的是登录还是登出时间class EComparator implements Comparator<Employee>{    public int flag;    public EComparator(int flag) {        this.flag = flag;    }    @Override    public int compare(Employee o1, Employee o2) {        // TODO Auto-generated method stub        if(flag==0){//flag为0时比较登录时间            return o1.in_time.compareTo(o2.in_time);        }else{//flag不为0时比较登出时间            return o1.out_time.compareTo(o2.out_time);        }    }}public class Main {    public static void main(String[] args) {        int n;        Scanner input = new Scanner(System.in);        n = input.nextInt();        Employee[] emp = new Employee[n];//设置一个数组放置对象们        for(int i=0;i<n;i++){            emp[i] = new Employee();            emp[i].name = input.next();            emp[i].in_time = input.next();            emp[i].out_time = input.next();        }        //进行两次排序        Arrays.sort(emp, new EComparator(0));        System.out.print(emp[0].name + " ");        Arrays.sort(emp, new EComparator(1));        System.out.print(emp[n-1].name);    }}
原创粉丝点击