题解 | #学英语#

学英语

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLong()) { // 注意 while 处理多个 case
            long a = in.nextLong();
            System.out.println(readMethod(a));
        }
    }

    //获取一个long型整数(小于等于12位)3位分割
    public static LinkedHashMap<String, Integer> getHashMap(long num) {
        LinkedHashMap<String, Integer> result = new LinkedHashMap<String, Integer>();
        result.put("billion", 0);
        result.put("million", 0);
        result.put("thousand", 0);
        result.put("", 0);


        result.put("billion", (int)(num / 1000000000));
        result.put("million", (int)(num % 1000000000 / 1000000));
        result.put("thousand", (int)(num % 1000000 / 1000));
        result.put("", (int)(num % 1000));

        return result;
    }

    //获取一个三位数的读法,245 --> two thousand and forty five;
    public static String getNum(int num) {
        HashMap<Integer, String> table = new HashMap<Integer, String>();

        table.put(0, "");
        table.put(1, "one");
        table.put(2, "two");
        table.put(3, "three");
        table.put(4, "four");
        table.put(5, "five");
        table.put(6, "six");
        table.put(7, "seven");
        table.put(8, "eight");
        table.put(9, "nine");
        table.put(10, "ten");
        table.put(11, "eleven");
        table.put(12, "twelve");
        table.put(13, "thirteen");
        table.put(14, "fourteen");
        table.put(15, "fifteen");
        table.put(16, "sixteen");
        table.put(17, "seventeen");
        table.put(18, "eighteen");
        table.put(19, "nineteen");
        table.put(20, "twenty");
        table.put(30, "thirty");
        table.put(40, "forty");
        table.put(50, "fifty");
        table.put(60, "sixty");
        table.put(70, "seventy");
        table.put(80, "eighty");
        table.put(90, "ninety");

        /*
         * 根据num的大小,分为百位为0 or [1-9],个位十位分为00,小于20,大于19
         * 一共6种情况
         */
        int num1 = num / 100;
        int num2 = num / 10 % 10;
        int num3 = num % 10;
        int num4 = num % 100;
        if (num1 == 0) {
            if (num4 < 20) {
                return " " + table.get(num4) + " ";
            } else {
                return " " + table.get(num2 * 10) + " " + table.get(num3) + " ";
            }
        } else {
            if (num4 == 0) {
                return " " + table.get(num1) + " hundred ";
            } else if (num4 > 0 && num4 < 20) {
                return " " + table.get(num1) + " hundred and " + table.get(num4) + " ";
            } else {
                return " " + table.get(num1) + " hundred and "  + table.get(
                           num2 * 10) + " " + table.get(num3) + " ";
            }
        }
    }

    //获取输入long型整数(小于等于12位)的读法
    public static  String readMethod(long num) {
        LinkedHashMap<String, Integer> map = getHashMap(num);
        Set<String> set = map.keySet();
        StringBuilder sb = new StringBuilder();

        for (String s : set) {
            int i = map.get(s);
            if (i != 0) {
                sb.append(getNum(i)).append(" ").append(s).append(" ");
            }
        }

        String result = sb.toString();
        result = result .replaceAll("\\s+", " ");
        result = result .replaceAll("^\\s", "");
        result = result .replaceAll("\\s$", "");
        return  result;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务