[LeetCode]47. Integer to English Words整数的读法

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 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"

 

Hint:

  1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
  2. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
  3. There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)

 

解法:按billion、million、thousand和hundred及以下依次处理即可。

class Solution {
public:
    string numberToWords(int num) {
        vector<string> unit = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
        vector<string> dec1 = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
        vector<string> dec2 = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
        vector<string> expr = { "Thousand", "Million", "Billion" };
        string res = "";
        for (int i = 9; i >= 0; i -= 3)
        {
            int div = num / (int)pow(10, i);
            if (div > 0 && div < 10)
            {
                res += unit[div - 1] + " ";
                if (i > 0) res += expr[i / 3 - 1] + " ";
            }
            else if (div >= 10 && div < 20)
            {
                res += dec1[div - 10] + " ";
                if (i > 0) res += expr[i / 3 - 1] + " ";
            }
            else if (div >= 20)
            {
                if (div >= 20 && div < 100)
                {
                    int bas = div / 10, reu = div % 10;
                    res += dec2[bas] + " ";
                    if (reu > 0) res += unit[reu - 1] + " ";
                    if (i > 0) res += expr[i / 3 - 1] + " ";                        
                }
                else if (div >= 100)
                {
                    int hun = div / 100, tne = div / 10 % 10, uni = div % 10;
                    res += unit[hun - 1] + " Hundred ";
                    if (tne == 1) res += dec1[div % 100 - 10] + " ";
                    else if (tne > 1 || uni > 0)
                    {
                        if(tne > 1) res += dec2[tne] + " ";
                        if (uni > 0) res += unit[uni - 1] + " ";
                    }
                    if (i > 0) res += expr[i / 3 - 1] + " ";
                }
            }
            num %= (int)pow(10, i);
            if (num <= 0) break;
        }
        if (res.empty()) res += "Zero ";
        res.resize(res.size() - 1);
        return res;
    }
};

改进:将输入整数用billion、million、thousand除后,所得的商均在0-999之间,因此可以将0-999之间的读法单独提出来,作为一个工具函数。

class Solution {
public:
    string numberToWords(int num) {
        if (num == 0) return "Zero";
        vector<string> expr = { "Thousand", "Million", "Billion" };
        string res = "";
        for (int i = 9; i >= 0; i -= 3)
        {
            int div = num / (int)pow(10, i);
            if (div > 0)
            {
                res += lessThanThousand(div);
                if (i > 0) res += expr[i / 3 - 1] + " ";
            }
            num %= (int)pow(10, i);
        }
        res.resize(res.size() - 1);
        return res;
    }
private:
    string lessThanThousand(int num)
    {
        vector<string> unit = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
        vector<string> decd = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
        string res = "";
        int hun = num / 100, ten = num / 10 % 10, uni = num % 10;
        if (hun > 0) res += unit[hun - 1] + " Hundred ";
        if (ten == 1) res += decd[ten * 10 + uni - 10] + " ";
        else if (ten > 1 || uni > 0)
        {
            if (ten > 1) res += decd[8 + ten] + " ";
            if (uni > 0) res += unit[uni - 1] + " ";
        }
        return res;
    }
};

 

posted @ 2015-10-29 11:11  AprilCheny  阅读(211)  评论(0编辑  收藏  举报