题解 | 简单错误记录

简单错误记录

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        ArrayList<String> sb = new ArrayList<>();
        while(in.hasNext()){
            String str = in.nextLine();
            sb.add(str);
        }
        HashMap<String, Integer> map = new HashMap<>();
        HashMap< Integer,String> map2 = new HashMap<>();
        int count = 1;
        for(int i = 0; i < sb.size(); i++){
            String s=sb.get(i);
            String[] strs = s.split(" ");
            int n = Integer.parseInt(strs[1]);
            String location;
            String []names=strs[0].split("\\\\");
            String name = names[names.length-1];
            if(name.length()>16){
                location = name.substring(name.length()-16);
            }else {
                location = name;
            }
            String address = location + " " + n;
            if(!map.containsKey(address)){
                map.put(address,1);
                map2.put(count,address);
                count++;
            }else {
                map.put(address,map.get(address)+1);
            }
        }
        int times=8 ;
        int start=1;
        if(count>7){
            start=count-8;
        }
        for (int i = start; i <count; i++) {
            System.out.println(map2.get(i)+" "+map.get(map2.get(i)));
            times--;
            if(times==0){
                break;
            }
        }
    }
}#两个哈希表来解决,对输入不停的分割处理,写的比较粗糙

全部评论

相关推荐

喵_coding:项目太烂了外卖+点评啊 而且寒假实习差不多到时候了 hc没多少了 要实在想要找那只能投投大厂试试了
点赞 评论 收藏
分享
01-14 12:34
门头沟学院 C++
牛马人的牛马人生:太暖心了啊 配环境是真烦
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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