题解 | #简单错误记录#

简单错误记录

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

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

// 参考了:https://blog.nowcoder.net/n/6eb9f37c83e94cf89df0c97bc7398355?f=comment
public class Main {
    public static void main(String[] args) {
        Map<String, Integer> map = new LinkedHashMap<>();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String inputLine = scanner.nextLine();
            if (inputLine == null || inputLine.trim().length() == 0) {
                scanner.close();
                break;
            }
            // 1.分割内容与行号
            String[] firstArr = inputLine.split(" ");
            String fname = firstArr[0].substring(firstArr[0].lastIndexOf("\\") + 1);
            fname = fname.substring(Math.max(fname.length() - 16, 0)) + " " + firstArr[1];
            // 2.向map中存数据
            int value = map.get(fname) == null ? 1 : map.get(fname) + 1;
            map.put(fname, value);
        }

        // 输出后8个
        int count = 0;
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            if (map.size() - count <= 8) {
                System.out.println(entry.getKey() + " " + entry.getValue());
            }
            ++count;
        }

    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务