接外包,有相关需求的可以联系我:Telegram | Email

273. Integer to English Words

该文章创建(更新)于09/13/2020,请注意文章的时效性!

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.

Example

  • Example 1:

    Input: 123
    Output: "One Hundred Twenty Three"

  • Example 2:

    Input: 12345
    Output: "Twelve Thousand Three Hundred Forty Five"

  • Example 3:

    Input: 1234567
    Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

  • Example 4:

    Input: 1234567891
    Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"

思路

从个位数开始向左,没三位划分一组,每一组都可看成一个“百位数”,除了第一组没有后缀外,从第二组开始的后缀依次为:{Thousand","Million","Billion"}

如输入的数字为:1234567891,则有划分1,234,567,891

1234567891
“百位数”OneTwo Hundred Thirty FourFive Hundred Sixty SevenEight Hundred Ninety One
组数4321
后缀BillionMillionThousand-
得到结果One BillionTwo Hundred Thirty Four MillionFive Hundred Sixty Seven ThousandEight Hundred Ninety One

最后把这些结果组合在一起即可!

Code

JAVA

获取对应字符到还好说,但" "(空白)把我给绕晕了好久!

Submit Code

class Solution {

        String [] Unit = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};// 0-10
        String [] Tens = {"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",};//11-19
        String [] Tens_x ={"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};//{20,30,40,50,60,70,80,90}
        String [] Big = {"Hundred","Thousand","Million","Billion"};//{100,1000,1000000,1000000000}

        public String numberToWords(int num) {
            String ret = "";

            //特殊值0的情况
            if(num == 0){
                return "Zero";
            }

            int Bil = num / 1000000000;
            num = num - Bil * 1000000000;
            if(Bil != 0){
                if(num == 0){       //若num为1000000000
                    ret = ret + GetStrOfThreeNumber(Bil) + " " + Big[3];
                    return ret;
                }else{
                    ret = ret + GetStrOfThreeNumber(Bil) + " " + Big[3] + " ";
                }
//                System.out.println(ret);
            }

            int Mil = num / 1000000;
            num = num - Mil * 1000000;
            if(Mil != 0){
                if(num == 0){   //若num为100000
                    ret = ret + GetStrOfThreeNumber(Mil) +" " +  Big [2];
                    return ret;
                }else {
                    ret = ret + GetStrOfThreeNumber(Mil) +" " +  Big [2] + " ";
                }
//                System.out.println(ret);
            }

            int Tho = num / 1000;
            num = num - Tho * 1000;
            if(Tho != 0){
                if(num == 0){  //若num为1000
                    ret = ret + GetStrOfThreeNumber(Tho) + " " + Big[1];
                    return ret;
                }else{
                    ret = ret + GetStrOfThreeNumber(Tho) + " " + Big[1] + " ";
                }
//                System.out.println(ret);
            }

            ret = ret + GetStrOfThreeNumber(num);

            return  ret;
        }

        //用于获取三位数之内的字符
        public String GetStrOfThreeNumber(int num){
            String ret = "";

            //百位数
            int Hun = num / 100;
            num = num - Hun * 100;
            if(Hun != 0){
                    if(num == 0){ //若num 为100
                        ret = ret + Unit[Hun - 1] + " " + Big[0];
                    }else{
                        ret = ret + Unit[Hun - 1] + " " + Big[0] + " ";
                    }
            }

            //十位数及个位数:num
            // 分三种大的情况:
            // num > 19; num ==> Tens_x[num/10] + Unit[num - (num/10) * 10](个位数不为0的情况下)
            // 10 < num <= 19; num ==> Tens[num - 10 - 1]
            // 0 < num <= 10; num ==> Unit[num - 1]
            if(num > 19){
                if(num % 10 == 0) {
                    ret = ret + Tens_x[num / 10 - 2];  //十倍整数的情况
                }else{ //非十倍整数的情况;
                    int Te = num / 10; //获取十位数(2 <= Te <= 9)
                    num = num - Te * 10;//获取个位数
                    ret = ret  + Tens_x[Te - 2] + " " + Unit[num - 1];
                }
            }else if(num > 10 && num <= 19){
                ret = ret + Tens[num - 11];
            }else if(num > 0 && num <= 10){
                ret = ret  + Unit[num -1];
            }

            return ret;
        }
}

完整测试代码

public class Integer_to_English_Words {
    static  public class Solution {

        String [] Unit = {"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten"};// 0-10
        String [] Tens = {"Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen",};//11-19
        String [] Tens_x ={"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"};//{20,30,40,50,60,70,80,90}
        String [] Big = {"Hundred","Thousand","Million","Billion"};//{100,1000,1000000,1000000000}

        public String numberToWords(int num) {
            String ret = "";

            //特殊值0的情况
            if(num == 0){
                return "Zero";
            }

            int Bil = num / 1000000000;
            num = num - Bil * 1000000000;
            if(Bil != 0){
                if(num == 0){       //若num为1000000000
                    ret = ret + GetStrOfThreeNumber(Bil) + " " + Big[3];
                    return ret;
                }else{
                    ret = ret + GetStrOfThreeNumber(Bil) + " " + Big[3] + " ";
                }
//                System.out.println(ret);
            }

            int Mil = num / 1000000;
            num = num - Mil * 1000000;
            if(Mil != 0){
                if(num == 0){   //若num为100000
                    ret = ret + GetStrOfThreeNumber(Mil) +" " +  Big [2];
                    return ret;
                }else {
                    ret = ret + GetStrOfThreeNumber(Mil) +" " +  Big [2] + " ";
                }
//                System.out.println(ret);
            }

            int Tho = num / 1000;
            num = num - Tho * 1000;
            if(Tho != 0){
                if(num == 0){  //若num为1000
                    ret = ret + GetStrOfThreeNumber(Tho) + " " + Big[1];
                    return ret;
                }else{
                    ret = ret + GetStrOfThreeNumber(Tho) + " " + Big[1] + " ";
                }
//                System.out.println(ret);
            }

            ret = ret + GetStrOfThreeNumber(num);

            return  ret;
        }

        //用于获取三位数之内的字符
        public String GetStrOfThreeNumber(int num){
            String ret = "";

            //百位数
            int Hun = num / 100;
            num = num - Hun * 100;
            if(Hun != 0){
                    if(num == 0){ //若num 为100
                        ret = ret + Unit[Hun - 1] + " " + Big[0];
                    }else{
                        ret = ret + Unit[Hun - 1] + " " + Big[0] + " ";
                    }
            }

            //十位数及个位数:num
            // 分三种大的情况:
            // num > 19; num ==> Tens_x[num/10] + Unit[num - (num/10) * 10](个位数不为0的情况下)
            // 10 < num <= 19; num ==> Tens[num - 10 - 1]
            // 0 < num <= 10; num ==> Unit[num - 1]
            if(num > 19){
                if(num % 10 == 0) {
                    ret = ret + Tens_x[num / 10 - 2];  //十倍整数的情况
                }else{ //非十倍整数的情况;
                    int Te = num / 10; //获取十位数(2 <= Te <= 9)
                    num = num - Te * 10;//获取个位数
                    ret = ret  + Tens_x[Te - 2] + " " + Unit[num - 1];
                }
            }else if(num > 10 && num <= 19){
                ret = ret + Tens[num - 11];
            }else if(num > 0 && num <= 10){
                ret = ret  + Unit[num -1];
            }

            return ret;
        }

    }


    public static  void  main(String [] args){
        Solution S = new Solution();
        int [] num = {1,12,88,123,1234,12345,123456,1234567,12345678,123456789,1234567891,0,10,20,100,1000,10000,100000,1000000000};
        // 打印所有数组元素
        for (int i = 0; i < num.length; i++) {
            System.out.println(num[i] + ":" + S.numberToWords(num[i])+"D");  //用D来判断最后一个字母之后是否有空格的存在?
        }
    }
}


要不赞赏一下?

微信
支付宝
PayPal
Bitcoin

版权声明 | Copyright

除非特别说明,本博客所有作品均采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。转载请注明转自-
https://www.emperinter.info/2020/09/13/integer-to-english-words/


要不聊聊?

我相信你准备留下的内容是经过思考的!【勾选防爬虫,未勾选无法留言】

*

*



微信公众号

优惠码

阿里云国际版20美元
Vultr10美元
搬瓦工 | Bandwagon应该有折扣吧?
域名 | namesiloemperinter(1美元)