洛谷 P1603 斯诺登的密码(JAVA)

洛谷 P1603 斯诺登的密码(JAVA)

package Algorithm;

import java.util.*;

public class Department1 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str = scan.nextLine();
        String[] li = str.split(" ");
        HashMap<String, Integer> hm = new HashMap<>();
        initialize(hm);
        for(int i = 0; i < li.length; i++) {
            if(hm.containsKey(li[i])) {
                hm.put(li[i], hm.get(li[i]) + 1);
            }
        }
        Set<Map.Entry<String, Integer>> entry = hm.entrySet();
        int[] integer = new int[6];
        Arrays.fill(integer, Integer.MAX_VALUE);
        int cnt = 0;
        for(Map.Entry<String, Integer> e: entry) {
            if(e.getValue() > 0) {
                int num = transform(e.getKey());
                integer[cnt++] = (num*num)%100;
            }
        }
        Arrays.sort(integer);
        if(integer[0] != Integer.MAX_VALUE) {
            str = "";
            boolean judge = true;
            for (int i = 0; i < cnt; i++) {
                int e = integer[i];
                if (judge) {
                    str += e;
                    judge = false;
                    continue;
                }
                if (e > 0 && e < 10) {
                    str += "0" + e;
                    continue;
                }
                str += e;
            }
            System.out.println(str);
        } else {
            System.out.println(0);
        }
    }

    private static int transform(String str) {
        int ans = 0;
        switch(str) {
            case "one":
            case "a":
            case "first": ans = 1; break;
            case "two":
            case "second":
            case "both": ans = 2; break;
            case "three":
            case"another":
            case "third": ans = 3; break;
            case "four": ans = 4; break;
            case "five": ans = 5; break;
            case "six": ans = 6; break;
            case "seven": ans = 7; break;
            case "eight": ans = 8; break;
            case "nine": ans = 9; break;
            case "ten": ans = 10; break;
            case "eleven": ans = 11; break;
            case "twelve": ans = 12; break;
            case "thirteen": ans = 13; break;
            case "fourteen": ans = 14; break;
            case "fifteen": ans = 15; break;
            case "sixteen": ans = 16; break;
            case "seventeen": ans = 17; break;
            case "eighteen": ans = 18; break;
            case "nineteen": ans = 19;  break;
            case "twenty": ans = 20; break;
        }
        return ans;
    }

    private static void initialize(HashMap<String, Integer> hm) {
        hm.put("one", 0); hm.put("eleven", 0);
        hm.put("two", 0); hm.put("twelve", 0);
        hm.put("three", 0); hm.put("thirteen", 0);
        hm.put("four", 0); hm.put("fourteen", 0);
        hm.put("five", 0); hm.put("fifteen", 0);
        hm.put("six", 0); hm.put("sixteen", 0);
        hm.put("seven", 0); hm.put("seventeen", 0);
        hm.put("eight", 0); hm.put("eighteen", 0);
        hm.put("nine", 0); hm.put("nineteen", 0);
        hm.put("ten", 0); hm.put("twenty", 0);
        hm.put("a", 0);
        hm.put("both", 0);
        hm.put("another", 0);
        hm.put("first", 0);
        hm.put("second", 0);
        hm.put("third", 0);
    }
}
上一篇:Java 统计字符串中每个字符出现的次数使用集合方法


下一篇:日志截取