题解 | #简单错误记录#

简单错误记录

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);
        LinkedHashMap<String, Integer> errormap = new LinkedHashMap<>();
        while (in.hasNextLine()) {
            String ins = in.nextLine();
            if (ins == null || ins.length() == 0) break;
            String s[] = ins.split(" ");
            String hanghao = s[1];
            String[] paths = s[0].split("\\\\");
            String name = paths[paths.length - 1];
            if (name.length() > 16) name = name.substring(name.length() - 16);
            String jilu = name + " " + hanghao;
            if (errormap.containsKey(jilu)) {
                int num = errormap.get(jilu);
                errormap.replace(jilu, num + 1);
            } else if (!errormap.containsKey(jilu)) {
                errormap.put(jilu, 1);
            }
        }
                //map获取特定的key比较麻烦,所以先把key都取出来放在arraylist中,然后再输出
        ArrayList<String> outlist = new ArrayList<String>();
        Iterator it = errormap.keySet().iterator();
        while (it.hasNext()) {
            String name = it.next().toString();
            outlist.add(name + " " + errormap.get(name));
        }
        if (outlist.size() <= 8) {
            for (String s : outlist) System.out.println(s);
        } else {
            for (int k = outlist.size() - 8; k < outlist.size(); k++) {
                System.out.println(outlist.get(k));
            }
        }

    }

}        

全部评论

相关推荐

挥毫自在:想白嫖你呢
点赞 评论 收藏
分享
notbeentak...:孩子,说实话,选择很重要,可能你换一个方向会好很多,但是现在时间不太够了,除非准备春招
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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