题解 | #学英语#

学英语

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLong()) { 
            long num = in.nextLong();
            if (num > 999) {
                String out = getNumEngExp(num);
                if(out.endsWith(" and ")) {
                    System.out.println(out.substring(0, out.length()-4));
                } else {
                    System.out.println(out);
                }
                
            } else {
                String out = num1To999(num);
                if(out.endsWith(" and ")) {
                    System.out.println(out.substring(0, out.length()-4));
                } else {
                    System.out.println(out);
                }
            }
        }
    }

    public static String num1To19(long num) {
        if (num < 1 || num > 19) {
            return "";
        }
        String[] names = {
                "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ","nine ",
                "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ",
                "eighteen ", "nineteen "
        };
        return names[(int)num-1];
    }

    public static String num1To99(long num) {
        if (num < 1 || num > 99) {
            return "";
        }
        if (num < 20) {
            return num1To19(num);
        }
        long high = num /10;
        String[] tyNames = {
                "twenty ", "thirty ", "forty ", "fifty ", "sixty ",
                "seventy ", "eighty ", "ninety "
        };
        return tyNames[(int)high-2] + num1To19(num % 10);
    }

    public static String num1To999(long num) {
        if (num < 1 || num > 999) {
            return "";
        }
        if (num < 100) {
            return num1To99(num);
        }
        long high = num /100;
        return num1To19(high) + "hundred and " + num1To99(num % 100);
    }

    public static String getNumEngExp(long num) {
        if (num == 0) {
            return "zero";
        }
        String res = "";
        if (num < 0) {
            res = "negative, ";
        }
        if (num == Long.MIN_VALUE) {
            res += "two billion, ";
            num %= -2 * 1000 * 1000 * 1000;
        }
        num = Math.abs(num);
        long high = 1 * 1000 * 1000 * 1000;
        int highIndex = 0;
        String[] names = {"billion", "million", "thousand"};
        while (num != 0) {
            long cur = num / high;
            num %= high;
            if (cur != 0) {
                res += num1To999(cur);
                if (highIndex < 3) {
                    res += names[highIndex] + " ";
                }
            }
            high /= 1000;
            highIndex++;
        }
        return res;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
今天 12:02
ssob上原来真有BOSS啊
硫蛋蛋:这种也是打工的,只不是是给写字楼房东打工
点赞 评论 收藏
分享
06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务