日志中每个记录是一个字符串,每个字符串占一行。最后一行为空行,表示日志结束。日志中最多可能有10000条记录。 计算任务名称的长度不超过10,开始执行时间的格式是YYYY-MM-DD HH:MM:SS,MMM,消耗时间小数点后有三位数字。 计算任务名称与任务开始时间、消耗时间之间以一个或多个空格隔开,行首和行尾可能有多余的空格。
排序好的日志记录。每个记录的字符串各占一行。 输入的格式与输入保持一致,输入包括几个空格,你的输出中也应该包含同样多的空格。
hs_10000_p 2007-01-17 19:22:53,315 253.035(s) hs_10001_p 2007-01-17 19:22:53,315 253.846(s) hs_10002_m 2007-01-17 19:22:53,315 129.574(s) hs_10002_p 2007-01-17 19:22:53,315 262.531(s) hs_10003_m 2007-01-17 19:22:53,318 126.622(s) hs_10003_p 2007-01-17 19:22:53,318 136.962(s) hs_10005_m 2007-01-17 19:22:53,318 130.487(s) hs_10005_p 2007-01-17 19:22:53,318 253.035(s) hs_10006_m 2007-01-17 19:22:53,318 248.548(s) hs_10006_p 2007-01-17 19:25:23,367 3146.827(s)
hs_10003_m 2007-01-17 19:22:53,318 126.622(s) hs_10002_m 2007-01-17 19:22:53,315 129.574(s) hs_10005_m 2007-01-17 19:22:53,318 130.487(s) hs_10003_p 2007-01-17 19:22:53,318 136.962(s) hs_10006_m 2007-01-17 19:22:53,318 248.548(s) hs_10000_p 2007-01-17 19:22:53,315 253.035(s) hs_10005_p 2007-01-17 19:22:53,318 253.035(s) hs_10001_p 2007-01-17 19:22:53,315 253.846(s) hs_10002_p 2007-01-17 19:22:53,315 262.531(s) hs_10006_p 2007-01-17 19:25:23,367 3146.827(s)
import java.text.ParseException; import java.util.*; import java.text.SimpleDateFormat; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class NKTEST { public static void main(String[] args)throws ParseException { Scanner in = new Scanner(System.in); ArrayList<myTime> arr = new ArrayList<myTime>(); // 注意 hasNext 和 hasNextLine 的区别 String s4, s1, s2, s3; myTime mytime ; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss,SS"); int n =10; while (n-- >0) { // 注意 while 处理多个 case arr.add(new myTime(in.next(), in.next(), in.next(), in.next())); } arr.sort(new Comparator<myTime>() { @Override public int compare(myTime t1, myTime t2) { String[] str1 = t1.getruntime().split("\\("); String[] str2 = t2.getruntime().split("\\("); if (Double.parseDouble(str1[0]) - Double.parseDouble(str2[0]) > 0) { return 1; } if (Double.parseDouble(str1[0]) - Double.parseDouble(str2[0]) < 0) { return -1; } try { if(sdf.parse(t1.getTime1()).getTime()+sdf1.parse(t1.getTime2()).getTime() > sdf.parse(t2.getTime1()).getTime()+sdf1.parse(t2.getTime2()).getTime()){ return 1; } } catch (ParseException e) { throw new RuntimeException(e); } return 0; } }); for(myTime m:arr){ m.daying(); } } } class myTime { String num; String time1; String time2; String runtime; public myTime(String num, String time1, String time2, String runtime) { this.num = num; this.time1 = time1; this.time2 = time2; this.runtime = runtime; } public String getTime1() { return time1; } public String getTime2() { return time2; } public String getruntime() { return runtime; } public void daying() { System.out.println(num + " " + time1 + " " + time2 + " " + runtime); } }面向对象咯
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; public class Main { static class mission { String mis; int y, m, d, h, mi, s, ms; double time; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; ArrayList<mission> list = new ArrayList<>(); while ((str = br.readLine()) != null) { mission log = new mission(); log.mis = str; String[] ss = str.split("\\s+");//按空白部分进行拆分,不管这个空白使用什么操作留的,提如空格键 tab键 String[] date = ss[1].split("-"); log.y = Integer.parseInt(date[0]); log.m = Integer.parseInt(date[1]); log.d = Integer.parseInt(date[2]); String[] t = ss[2].split(":"); log.h = Integer.parseInt(t[0]); log.mi = Integer.parseInt(t[1]); String[] ms = t[2].split(","); log.s = Integer.parseInt(ms[0]); log.ms = Integer.parseInt(ms[1]); String tim = ss[3].substring(0, ss[3].indexOf("(")); log.time = Double.parseDouble(tim); list.add(log); } list.sort(new Comparator<mission>() { @Override public int compare(mission o1, mission o2) { int num1 = 0; if (o1.time - o2.time > 0) num1 = 1; if (o1.time - o2.time < 0) num1 = -1; int num2 = num1 == 0 ? o1.y - o2.y : num1; int num3 = num2 == 0 ? o1.m - o2.m : num2; int num4 = num3 == 0 ? o1.d - o2.d : num3; int num5 = num4 == 0 ? o1.h - o2.h : num4; int num6 = num5 == 0 ? o1.mi - o2.mi : num5; int num7 = num6 == 0 ? o1.s - o2.s : num6; int num8 = num7 == 0 ? o1.ms - o2.ms : num7; return num8; } }); for (mission mm : list) System.out.println(mm.mis); } }
import java.util.ArrayList; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<String> list = new ArrayList<>(); while (scanner.hasNext()){ String line = scanner.nextLine(); list.add(line); } list.sort(Comparator.comparing(Main::getTime)); for (String s : list) System.out.println(s); } static Double getTime(String s){ String[] s1 = s.split("\\s+"); String ss = s1[3].substring(0, s1[3].indexOf("(s)")); return Double.parseDouble(ss); } }
import java.util.*; import java.time.LocalDate; import java.time.LocalTime; public class Main { static class Log implements Comparable<Log> { private String name; private String date; private String time; private String duration; private StringBuilder space1; private StringBuilder space2; private double EPSILON = 1e-4; public Log(String record) { String[] str = record.split(" "); name = str[0]; duration = str[str.length-1]; space1 = new StringBuilder(); space2 = new StringBuilder(); int i = 1, j = str.length-2; while (str[i].isEmpty() && str[j].isEmpty()) { space1.append(" "); space2.append(" "); ++i; --j; } while (i < j && str[i].isEmpty()) { space1.append(" "); ++i; } space1.append(" "); date = str[i]; while (i < j && str[j].isEmpty()) { space2.append(" "); --j; } time = str[j]; space2.append(" "); } public String getName() { return name; } public String getDate() { return date; } public String getTime() { return time; } public String getDuration() { return duration; } public String getTimeStamp() { return getDate() + " " + getTime(); } public String getSpace1() { return space1.toString(); } public String getSpace2() { return space2.toString(); } public int compareDate(Log other) { try { LocalDate ld1 = LocalDate.parse(getDate()); LocalTime lt1 = LocalTime.parse(getTime().replace(",", ".")); LocalDate ld2 = LocalDate.parse(other.getDate()); LocalTime lt2 = LocalTime.parse(other.getTime().replace(",", ".")); if (ld1.compareTo(ld2) == 0) { return lt1.compareTo(lt2); } else { return ld1.compareTo(ld2); } } catch (Exception e) { return getDate().compareTo(other.getDate()); } } public int compareTo(Log other) { double d1 = Double.valueOf(getDuration().split("\\(")[0]); double d2 = Double.valueOf(other.getDuration().split("\\(")[0]); if (Math.abs(d1 - d2) < EPSILON) { return compareDate(other); } else if (d1 - d2 < 0) { return -1; } else { return 1; } } public String toString() { return getName()+getSpace1()+getTimeStamp()+getSpace2()+getDuration(); } } public static void main(String[] args) { Scanner reader = new Scanner(System.in); ArrayList<Log> logs = new ArrayList<>(); while (reader.hasNextLine()) { String record = reader.nextLine(); if(record.trim().isEmpty()) break; logs.add(new Log(record)); } Collections.sort(logs); for (Log l: logs) { System.out.println(l); } } }
我就想知道,下面的两段代码有啥区别? public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str_log; while ((str_log = sc.nextLine()) != null && str_log.length() != 0) { /* .... */ } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str_log; while (sc.hasNextLine()) { str_log = sc.nextLine(); if (str_log == null || str_log.length() == 0) { break; } /* ... ... */ } }
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.Scanner; /** * Created by fhqplzj on 17-2-1 at 下午1:35. */ class WebLog implements Comparable<WebLog> { /*日志*/ private String log; /*开始时间*/ private Date start; /*解析时间*/ private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS"); /*消耗时间*/ private Double elapsed; public WebLog(String log) { this.log = log; String[] parts = log.trim().split(" +"); try { this.start = sdf.parse(parts[1] + " " + parts[2]); } catch (ParseException e) { e.printStackTrace(); } this.elapsed = Double.parseDouble(parts[3].substring(0, parts[3].length() - 3)); } @Override public int compareTo(WebLog o) { if (!elapsed.equals(o.elapsed)) { return elapsed.compareTo(o.elapsed); } else { return start.before(o.start) ? -1 : (start.after(o.start) ? 1 : 0); } } @Override public String toString() { return this.log; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<String> logs = new ArrayList<>(); while (scanner.hasNextLine()) { String s = scanner.nextLine(); if (s.trim().isEmpty()) { break; } logs.add(s); } ArrayList<WebLog> webLogs = new ArrayList<>(logs.size()); for (String log : logs) { webLogs.add(new WebLog(log)); } Collections.sort(webLogs); for (WebLog webLog : webLogs) { System.out.println(webLog); } } }