题解 | #学英语#

学英语

https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc

import java.util.*;

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

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("1", "one");
        hashMap.put("2", "two");
        hashMap.put("3", "three");
        hashMap.put("4", "four");
        hashMap.put("5", "five");
        hashMap.put("6", "six");
        hashMap.put("7", "seven");
        hashMap.put("8", "eight");
        hashMap.put("9", "nine");

        HashMap<String, String> tenthMap = new HashMap<>();
//        tenthMap.put("1", "one");
        tenthMap.put("2", "twenty");
        tenthMap.put("3", "thirty");
        tenthMap.put("4", "forty");
        tenthMap.put("5", "fifty");
        tenthMap.put("6", "sixty");
        tenthMap.put("7", "seventy");
        tenthMap.put("8", "eighty");
        tenthMap.put("9", "ninety");

        HashMap<String, String> tenthsMap = new HashMap<>();
        tenthsMap.put("0", "ten");
        tenthsMap.put("1", "eleven");
        tenthsMap.put("2", "twelve");
        tenthsMap.put("3", "thirteen");
        tenthsMap.put("4", "fourteen");
        tenthsMap.put("5", "fifteen");
        tenthsMap.put("6", "sixteen");
        tenthsMap.put("7", "seventeen");
        tenthsMap.put("8", "eighteen");
        tenthsMap.put("9", "nineteen");

        while (str.length() % 3 != 0) {
            str = "0" + str;
        }
        StringBuilder stringBuilder = new StringBuilder();
        int length = str.length();
        List<StringBuilder> list = new ArrayList<>();
        for (int i = length; i >= 0; i--) {
            boolean flag = false;
            String subString = str.substring(i - 3, i);
            int localResult = Integer.parseInt(subString);
            StringBuilder st = new StringBuilder();
            if (localResult != 0) {
                int digit = localResult % 10;
                localResult = localResult / 10;
                if (localResult != 0) {
                    int tenth = localResult % 10;
                    localResult = localResult / 10;
                    if (localResult != 0) {
                        int hundred = localResult % 10;
                        st.append(hashMap.get(String.valueOf(hundred))).append(" hundred ");
                        if (tenth != 0 || digit != 0) st.append("and ");
                    }
                    if (tenth >= 2) {
                        st.append(tenthMap.get(String.valueOf(tenth))).append(" ");
                        if (digit != 0) {
                            st.append(hashMap.get(String.valueOf(digit))).append(" ");
                            flag = true;
                        }
                    } else if (tenth == 1) {
//                        st.append("and ");
                        st.append(tenthsMap.get(String.valueOf(digit)));
                        flag = true;
                    } else if (digit != 0) {
                        st.append(hashMap.get(String.valueOf(digit))).append(" ");
                        flag = true;
                    }
                }
                if (!flag && digit != 0) st.append(hashMap.get(String.valueOf(digit))).append(" ");
            }
            list.add(st);
            i = i - 2;
            if (i - 1 <= 0) break;
        }
        String result = "";
        int listLength = list.size();
        result = list.get(0).toString();
        int i = 1;
        if (i < listLength) {
            result = list.get(i).toString() + "thousand " + result;
            i ++;
            if (i < listLength) {
                result = list.get(i).toString() + "million " + result;
                i ++;
                if (i < listLength) {
                    result = list.get(i).toString() + "billion " + result;
                }
            }
        }
        System.out.println(result);
    }
}

全部评论

相关推荐

10-07 20:48
门头沟学院 Java
听说改名就会有offer:可能是实习上着班想到后面还要回学校给导师做牛马,看着身边都是21-25的年纪,突然emo了了
点赞 评论 收藏
分享
废铁汽车人:秋招真是牛鬼蛇神齐聚一堂
点赞 评论 收藏
分享
美团 后端开发 总包n(15%是股票)
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务