一个字符串中只有大小写字母和整数,输出字符串中的出现最多的数字的和?例如 ” 9fil3dj11P0jAsf11j ” 中出现最多的是11两次,输出22.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

//定义一个字符串常量,字符串中只有大小写字母和整数,输出字符串中的出现最多的数字的和?例如 ” 9fil3dj11P0jAsf11j ” 中出现最多的是11两次,输出22
public class Try96 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
        Scanner sc = new Scanner(System.in); 
        String s1 = sc.nextLine();
        String[] ex = s1.split("\\D+"); 
        if(ex.length==0) return;
        //System.out.println(ex[4]); 
        for(int i=0;i<ex.length;i++){
            int temp=Integer.parseInt(ex[i]);
            if(map.containsKey(temp)){
                int buffer=map.get(temp);
                
                map.put(temp, ++buffer);
            }
            else{
                map.put(temp, 1);
            }
        }
         List<Map.Entry<Integer,Integer>> list = new ArrayList<Map.Entry<Integer,Integer>>(map.entrySet());
            Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>() {
                //升序排序
                public int compare(Entry<Integer, Integer> o1,
                        Entry<Integer, Integer> o2) {
                    return o2.getValue().compareTo(o1.getValue());
                }
                
            });
            Entry<Integer, Integer> mapping=list.get(0);
            int jiguo=mapping.getKey()*mapping.getValue();
            System.out.println(jiguo); 
    }

}
 

猜你喜欢

转载自blog.csdn.net/boguesfei/article/details/81915459