题解 | 简单错误记录

简单错误记录

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++;
         }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
03-20 22:18
FightingNa...:小厂不喜欢离毕业还远的。培养你三个月小半年,你又回去上学,你丰富简历爽歪歪,小厂啥也得不到。大厂兴许愿意培养你,可以试试大厂,准备下不黑了就行。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务