题解 | #简单错误记录#

简单错误记录

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

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别

        Map<String, Integer> result = new LinkedHashMap<String, Integer>();
        while (in.hasNext()) { // 注意 while 处理多个 case

            String input = in.nextLine();
 
            String inputfile = input.split(" ")[0];// 文件名
            String inputfNum = input.split(" ")[1]; // 行数

            // 斜杆最后的文件
            inputfile = inputfile.substring(inputfile.lastIndexOf("\\")+1);
            inputfile = inputfile.length() > 16 ? inputfile.substring(inputfile.length() - 16) : inputfile;

            String key = inputfile.concat(" ").concat(inputfNum);
            boolean f = true;
             for (String rkey: result.keySet()) { 
                 if( rkey.equals(key) ){
                      result.put(rkey, result.get(key)+1); // 如果存在相同的就加1
                      f = false;
                      break;
                 } else {
                      f = true; 
                 }
             }

             if(f){
                 result.put(key,1);
             }

        }

        if(result.keySet().size() <= 8){

            for(String k : result.keySet()){
                System.out.println(k.concat(" ").concat(result.get(k)+"")); 
            }

        } else {

            List<String> cresultList = new ArrayList<>();

             for(String k : result.keySet()){
                cresultList.add(k.concat(" ").concat(result.get(k)+"")); 
            }

            for(int i=cresultList.size()-8;i < cresultList.size() ; i ++){
                System.out.println(cresultList.get(i)); 

            }

            


        }
          

        
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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