题解 | #简单错误记录#,最难的不是题,是题目说的不明不白

简单错误记录

http://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<String> list = new ArrayList<>();
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            if (line.equals("")) {
                break;
            }
            list.add(line);
        }
        LinkedHashMap<String, Integer> map = new LinkedHashMap<>();

        for (int i = 0; i < list.size(); i++) {
            String[] split = list.get(i).split(" ");
            String filePath = split[0];
            String[] strings = filePath.split("\\\\");
            String fileName = strings[strings.length - 1];
            if (fileName.length() > 16) {
                fileName = fileName.substring(fileName.length() - 16);
            }
            // 文件名相同,行数不同,不算同一记录
            String key = fileName + " " + split[1];
            Integer count = map.get(key);
            if (count == null) {
                map.put(key, 1);
            } else {
                map.put(key, map.get(key) + 1);
            }

        }

        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        Set<String> keySet = map.keySet();
        int num = 0;
        if (keySet.size() > 8) {
            for(String string : map.keySet()){
                num++;
                // *******
                if( num > (map.keySet().size()-8)) //输出最后八个记录
                    System.out.println(string+" "+map.get(string));
            }
        } else {
            for (Map.Entry<String, Integer> entry : entrySet) {
                System.out.println(entry.getKey() + " " + entry.getValue());
            }
        }
    }
}
全部评论
说的很对,这题我看了半天都没看懂,就是那个循环记录又说以第一次为准,例题又说不会被记录,搞不懂是什么意思。
点赞 回复 分享
发布于 2022-05-30 22:13

相关推荐

04-17 23:48
西北大学 Java
陈好好wy:加油加油 字节和心脏谁先跳动
字节跳动开奖383人在聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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