题解 | #学英语#

学英语

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

import java.util.*;

public class Main {
    public static final Map<Integer, String> map = new HashMap<Integer, String>() {
        {
            put(1, "one");
            put(2, "two");
            put(3, "three");
            put(4, "four");
            put(5, "five");
            put(6, "six");
            put(7, "seven");
            put(8, "eight");
            put(9, "nine");
            put(10, "ten");
            put(11, "eleven");
            put(12, "twelve");
            put(13, "thirteen");
            put(14, "fourteen");
            put(15, "fifteen");
            put(16, "sixteen");
            put(17, "seventeen");
            put(18, "eighteen");
            put(19, "nineteen");
            put(20, "twenty");
            put(30, "thirty");
            put(40, "forty");
            put(50, "fifty");
            put(60, "sixty");
            put(70, "seventy");
            put(80, "eighty");
            put(90, "ninety");
        }
    };

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            long number = sc.nextLong();
            if (number < 100) {
                System.out.println(getTenDigit((int) number));
            } else if (number < 1000) {
                System.out.println(getHundredDigit((int) number));
            } else if (number < 1000000) {
                System.out.println(getThousandDigit((int) number));
            } else if (number < 100000000) {
                System.out.println(getMillionDigit((int) number));
            } else {
                System.out.println(getBillionDigit(number));
            }
        }
        sc.close();
    }

    private static String getBillionDigit(long number) {
        int billion = (int) (number / 100000000);
        int million = (int) (number % 100000000);
        String result = getRangeResult(billion, " billion");
        if (million == 0) {
            return result;
        }
        if (million < 100) {
            return result + " " + getTenDigit(million);
        } else if (million < 1000) {
            return result + " " + getHundredDigit(million);
        } else if (million < 1000000) {
            return result + " " + getThousandDigit(million);
        }else {
            return result + " " + getMillionDigit(million);
        }
    }

    private static String getMillionDigit(long number) {
        int million = (int) (number / 1000000);
        String result = getRangeResult(million, " million");
        int thousand = (int)( number % 1000000);
        if (thousand == 0) {
            return result;
        }
        if (thousand < 100) {
            return result + " " + getTenDigit(thousand);
        } else if (thousand < 1000) {
            return result + " " + getHundredDigit(thousand);
        } else {
            return result + " " + getThousandDigit(thousand);
        }
    }

    private static String getRangeResult(int value, String sign) {
        String result;
        if (value < 100) {
            result = getTenDigit(value) + sign;
        } else {
            result = getHundredDigit(value) + sign;
        }
        return result;
    }

    private static String getThousandDigit(int number) {
        int thousand = number / 1000;
        int hundred = number % 1000;
        String result = getRangeResult(thousand, " thousand");
        if (hundred == 0) {
            return result;
        }
        if (hundred < 100) {
            return result + " " + getTenDigit(hundred);
        }
        return result + " " + getHundredDigit(hundred);

    }

    private static String getHundredDigit(int number) {
        int hundred = number / 100;
        int ten = number % 100;
        String result = map.get(hundred) + " hundred";
        if (ten == 0) {
            return result;
        }
        return result + " and " + getTenDigit(ten);
    }

    private static String getTenDigit(int number) {
        if (number <= 20) {
            return map.get(number);
        }
        int range = 20;
        while (number >= range + 10 && range < 100) {
            range = range + 10;
        }
        if (number % range == 0) {
            return map.get(range);
        }
        return map.get(range) + " " + map.get(Integer.parseInt(String.valueOf(number).substring(1)));
    }
}
全部评论

相关推荐

拒绝无效加班的小师弟很中意你:求职意向没有,年龄、课程冗余信息可以删掉,需要提升项目经历。排版需要修改。
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务