其他分享
首页 > 其他分享> > OD 机试 字符串字母出现统计排序

OD 机试 字符串字母出现统计排序

作者:互联网

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 输入
        String str = in.nextLine();
        //
        if(str.length() <= 0) {
            return;
        }
        char[] chr = str.toCharArray();
        // 循环解析
        Arrays.sort(chr);// TODO
        System.out.println(Arrays.toString(chr));
        // map
        HashMap<Character, Integer> map1 = new HashMap<>();
        for (int i = 0; i < chr.length; i++) {
            if (map1.get(chr[i]) == null) {
                map1.put(chr[i], 1);
            } else {
                map1.put(chr[i], map1.get(chr[i]) + 1);
            }
        }
        StringBuilder sb = new StringBuilder();
        int num = 0;
        for (Character ch : map1.keySet()) {
            if (ch >= 'a' && ch <= 'z') {
                System.out.println(ch + " " + map1.get(ch));
                sb.append(ch + ":" + map1.get(ch)+";");
            }
        }
        for (Character ch : map1.keySet()) {
            if (ch >= 'A' && ch <= 'Z') {
                System.out.println(ch + " " + map1.get(ch));
                sb.append(ch + ":" + map1.get(ch)+";");
            }
        }
        System.out.println(sb.toString());
    }
}

 

 

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);        
        // 输入
        String str = in.nextLine();
        //
        if(str.length() <= 0) {
            return;
        }
        char[] chr = str.toCharArray();
        // 循环解析
        Arrays.sort(chr);
        System.out.println(Arrays.toString(chr));
        // map
        HashMap<Character, Integer> map1 = new HashMap<>();
        for (int i = 0; i < chr.length; i++) {
            if (map1.get(chr[i]) == null) {
                map1.put(chr[i], 1);
            } else {
                map1.put(chr[i], map1.get(chr[i]) + 1);
            }
        }

        // map
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < chr.length; i++) {
            if (map.get(chr[i]) == null) {
                map.put(chr[i], 1);
            } else {
                map.put(chr[i], map.get(chr[i]) + 1);
            }
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (Character ch: map.keySet()) {
            Map<String, Object> mp = new HashMap<>();
            mp.put("key", ch);
            mp.put("num", map.get(ch));
            list.add(mp);
        }
        // 排序
        list.sort(new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                //
                int a = (int)o2.get("num")-(int)o1.get("num");
                // 等于
                if (a != 0) {
                    return a;
                }
                Character c1 = (Character)o1.get("key");
                Character c2 = (Character)o2.get("key");
                if (c1 >= 'a' && c2 < c1) {
                    return -1;
                }
                if (c2 >= 'a' && c1 < c1) {
                    return 1;
                }
                return c1-c2;
            }
        });
        StringBuilder stringBuilder = new StringBuilder();
        for (Map mp: list) {
            stringBuilder.append(mp.get("key") + ":" + mp.get("num")+";");
        }
        System.out.println(stringBuilder.toString());
    }
}

 

标签:get,int,OD,chr,map1,new,put,机试,排序
来源: https://www.cnblogs.com/supperlhg/p/16299887.html