题解 | 简单错误记录
简单错误记录
https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
private static String getFileName(String path) {
String fileName = path.substring(path.lastIndexOf("\\") + 1);
if (fileName.length() > 16) {
fileName = fileName.substring(fileName.length() - 16);
}
return fileName;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<String,Integer> map=new LinkedHashMap<>();
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String str=in.nextLine();
if("exit".equalsIgnoreCase(str)){
break;
}
if(str.trim().isEmpty()){
continue;
}
String[] arrs=str.split("\\s+",2);
String path=arrs[0];
String fileName=getFileName(path);
int fileNum;
try{
fileNum=Integer.parseInt(arrs[1]);
}catch(NumberFormatException e){
continue;
}
String key=fileName+" "+fileNum;
map.put(key,map.getOrDefault(key,0)+1);
}
int flag=0;
for(Map.Entry<String,Integer> en:map.entrySet()){
if(map.size()-flag<=8){
System.out.println(en.getKey()+" "+en.getValue());
}
flag++;
}
}
}
查看11道真题和解析