题解 | #学英语#

学英语

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

全部评论

相关推荐

11-13 20:16
已编辑
厦门理工学院 软件测试
专业嗎喽:硕佬,把学校背景放后面几段,学校背景双非还学院,让人看了就不想往下看。 把实习经历和个人奖项放前面,用数字化简述自己实习的成果和掌握的技能,比如负责项目一次通过率90%,曾4次发现项目潜在问题风险为公司减少损失等等
点赞 评论 收藏
分享
大野鸡:其实就是量,但是时间有限,1000题只要不是全中等简单,简单中等困难1-2-1,大概能打打比赛了(前20%),10000题就是下一个灵神
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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