首页 > 其他 > 详细

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

时间:2015-10-29 12:50:13      阅读:274      评论:0      收藏:0      [点我收藏+]

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

 

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

原文:http://www.cnblogs.com/aprilcheny/p/4919849.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!