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:
每个输入文件包含一个测试用例。每个案例都包含一天的记录。大小写以一个正整数M开头,M是记录的总数,后面跟着M行,每行的格式如下:
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.
其中时间的格式为HH:MM:SS,ID_number是一个不超过15个字符的字符串。
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
思路:用map存储每一个id的进入时间和退出时间,最后再比较找出最早的和最晚的时间,麻烦的地方就在于怎么样比较时间合适,我想到的方法就是将小时、分钟、秒分开存储,逐一进行比较,如果小时数一样,就比较分钟数。。。
不多说了,直接上代码
import java.util.*;
class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Map<String, List<List<Integer>>> map = new HashMap<>();
while(n-- > 0)
{
String id = sc.next();
String []sign_in = sc.next().split(":");//进入时间
String []sign_out = sc.next().split(":");//退出时间
ArrayList<List<Integer>> w = new ArrayList<>();
map.put(id,w);
ArrayList<Integer> in = new ArrayList<>();
for(int i = 0 ; i < 3 ;i++)
{
in.add(Integer.parseInt(sign_in[i]));
}
w.add(in);
ArrayList<Integer> out = new ArrayList<>();
for(int i = 0 ; i < 3 ;i ++)
{
out.add(Integer.parseInt(sign_out[i]));
}
w.add(out);
}
String earlist_id = "";
int []earlist_time = {23,60,60};//假设最晚时间是23:60:60
String latest_id = "";
int []latest_time = {0,0,0};//假设最早时间是0:0:0
for(String s : map.keySet())
{
if(map.get(s).get(0).get(0) < earlist_time[0])
{
earlist_time[0] = map.get(s).get(0).get(0);
earlist_id = s;
}
else if(map.get(s).get(0).get(0) == earlist_time[0])
{
if(map.get(s).get(0).get(1) < earlist_time[1])
{
earlist_time[1] = map.get(s).get(0).get(1);
earlist_id = s;
}
else if(map.get(s).get(0).get(1) == earlist_time[1])
{
if(map.get(s).get(0).get(2) < earlist_time[2])
{
earlist_time[2] = map.get(s).get(0).get(2);
earlist_id = s;
}
}
}
}
System.out.print(earlist_id + " ");
for(String s : map.keySet())
{
if(map.get(s).get(1).get(0) > latest_time[0])
{
latest_time[0] = map.get(s).get(1).get(0);
latest_id = s;
}
else if(map.get(s).get(1).get(0) == earlist_time[0])
{
if(map.get(s).get(1).get(1) < earlist_time[1])
{
latest_time[1] = map.get(s).get(1).get(1);
latest_id = s;
}
else if(map.get(s).get(1).get(2) == earlist_time[2])
{
if(map.get(s).get(1).get(2) < earlist_time[2])
{
latest_time[2] = map.get(s).get(1).get(2);
latest_id = s;
}
}
}
}
System.out.print(latest_id);
}
}