pat甲级【1006 Sign In and Sign Out】
考查排序:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; public class Main { @SuppressWarnings("unchecked") public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int m = Integer.valueOf(br.readLine()); String[][] records = new String[m][3]; for (int i = 0; i < m; i++) { String[] words = br.readLine().split("\\s+"); records[i] = words; } Arrays.sort(records, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { String signin1 = o1[1]; String signin2 = o2[1]; int[] time1 = extract(signin1); int[] time2 = extract(signin2); int h1=time1[0],m1=time1[1],s1=time1[2]; int h2=time2[0],m2=time2[1],s2=time2[2]; int t1 = h1*3600 + m1*60 + s1; int t2 = h2*3600 + m2*60 + s2; return t1 - t2; } }); String s1 = records[0][0]; Arrays.sort(records, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { String signout1 = o1[2]; String signout2 = o2[2]; int[] time1 = extract(signout1); int[] time2 = extract(signout2); int h1=time1[0],m1=time1[1],s1=time1[2]; int h2=time2[0],m2=time2[1],s2=time2[2]; int t1 = h1*3600 + m1*60 + s1; int t2 = h2*3600 + m2*60 + s2; return t2 - t1; } }); String s2 = records[0][0]; System.out.println(s1 + " "+ s2); br.close(); } public static int[] extract(String str){ String[] words = str.split(":"); int[] arr = new int[3]; arr[0] = Integer.valueOf(words[0]); arr[1] = Integer.valueOf(words[1]); arr[2] = Integer.valueOf(words[2]); return arr; } }

浙公网安备 33010602011771号