电话聊天狂人Java

电话聊天狂人Java,第1张

编程语言:Java
题目:

题解:不超时需要用技巧。
结果:AC

import java.io.*;
import java.util.*;
public class Main {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static Scanner sc = new Scanner(new BufferedInputStream(System.in));



    public static void main(String[] args) throws IOException {
        in.nextToken();
        int n= (int) in.nval;
        Map<String,Integer> map=new LinkedHashMap<>();
        String t;
        for(int i=0;i<n;i++){
            in.nextToken();
            t=in.nval+"";
            if(map.containsKey(t))
                map.put(t,(int)map.get(t)+1);
            else
                map.put(t,1);
            in.nextToken();
            t=in.nval+"";
            if(map.containsKey(t))
                map.put(t,(int)map.get(t)+1);
            else
                map.put(t,1);
        }
        int sum=0;
        int num=0;
        String res="";
        Iterator<Map.Entry<String, Integer>> entries = map.entrySet().iterator();
        while(entries.hasNext()){
            Map.Entry<String, Integer> entry = entries.next();
            if(entry.getValue()>sum){
                res=entry.getKey();
                num=1;
                sum=entry.getValue();
            }else if(entry.getValue()==sum){
                num++;
                if(res.compareTo(entry.getKey())>0){
                    res=entry.getKey();
                }
            }
        }
        res=res.substring(0,1)+res.substring(2,12);
        if(num==1){
            out.println(res+" "+sum);
        }else{
            out.println(res+" "+sum+" "+num);
        }
        out.flush();
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/905853.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存