题解 | #学英语#

学英语

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);
    }
}

全部评论

相关推荐

感性的干饭人在线蹲牛友:🐮 应该是在嘉定这边叭,禾赛大楼挺好看的
点赞 评论 收藏
分享
牛客263158796号:我领羊一面后十天不挂也不推进 今天问hr说等前序的第一批意向发完看情况再看是否推进
点赞 评论 收藏
分享
小红书 后端开发 总包n+8w+期权
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务