[leetcode] 273.Integer to English Words

题目:
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 2^31 - 1.

For example,
123 -> “One Hundred Twenty Three”
12345 -> “Twelve Thousand Three Hundred Forty Five”
1234567 -> “One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven”
题意:
给定一个字符串表示的非负整数,使用英文描述它。例子参见上面。
思路:
我们发现其实是按照每三位进行读的,比如1234567,最后567就是三位,然后是234,234读完后在后面加一个”Thousand”即可,同理对于”Million”,”Billion”都是这样的读法。
代码如下:

class Solution {
public:
    string numberToWords(int num) {
        if (num == 0)return "Zero";
        string num1[] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
        string num2[] = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
        string tens[] = { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
        string units[] = { "Thousand", "Million", "Billion" };
        int thousand = 1;
        int count = -1;

        while (num / thousand >= 1000) {
            thousand *= 1000;
            count++;
        }

        string result = "";
        while (num > 0) {
            int n = num / thousand;
            if (n > 0) {
                if (result != "")result += " ";
                result += hundred2Words(n, num1, num2, tens);
                if (count >= 0){
                    if (result != "")result += " ";
                    result += units[count];
                }
            }

            num = num % thousand;
            thousand /= 1000;
            count--;
        }
        return result;
    }

    string hundred2Words(int num, string num1[], string num2[], string tens[]) {
        int pow = 100;
        string result = "";
        while (num > 0) {
            int n = num / pow;
            if (n != 0) {
                switch (pow){
                case 100: {
                              if (result != "")
                                  result += " ";
                              result = result + num1[n - 1] + " " + "Hundred";
                              break;
                }
                case 10: {
                             if (result != "")result += " ";
                             if (n == 1){
                                 result += num2[num - 10];
                                 return result;
                             }
                             else {
                                 result += tens[n - 2];
                             }
                             break;
                }
                case 1: {
                            if (result != "")result += " ";
                            result += num1[n - 1];
                            break;
                }
                }
            }

            num %= pow;
            pow /= 10;
        }
        return result;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值