题解 | #学英语#

学英语

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

全部评论

相关推荐

爱喝奶茶的垂耳兔拥抱太阳:感觉项目和实习没有技术亮点和难点,单纯说了自己干了啥
点赞 评论 收藏
分享
05-12 11:09
已编辑
门头沟学院 后端
已注销:没必要放这么多专业技能的描述。这些应该是默认已会的,写这么多行感觉在凑内容。项目这块感觉再包装包装吧,换个名字,虽然大家的项目基本都是网上套壳的,但是你这也太明显了。放一个业务项目,再放一个技术项目。技术项目,例如中间件的一些扩展和尝试。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务