题解 | #简单错误记录#
简单错误记录
https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Map<String, Integer> map = new LinkedHashMap<>(); List<String> resList=new ArrayList<>(); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String s = in.next(); //取行号 int num = in.nextInt(); //分隔后取最后一个 String[] path = s.split("\\\\"); //取文件名 String name = path[path.length - 1]; if (name.length() > 16) { //取后16位 name = name.substring(name.length() - 16, name.length()); } //文件名和行号拼接 String record = name + " " + num; //System.out.println(record); //放入HashMap Integer count = map.get(record); if (count == null) { map.put(record, 1); } else { map.put(record, count + 1); } } //遍历hashMap Set<String> set = map.keySet(); for (String s1 : set) { // System.out.println(s1+" "+map.get(s1)); String res = s1 + " " + map.get(s1); resList.add(res); } int j=0; if(resList.size()>8){ j=resList.size()-8; } for(int i=j;i<resList.size();i++){ System.out.println(resList.get(i)); } } }