码迷,mamicode.com
首页 > 其他好文 > 详细

273. Integer to English Words

时间:2018-06-17 11:08:01      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:行存储   color   represent   ber   vat   for   之间   count   turn   

问题描述:

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

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"

 

解题思路:

我们可以来看一下0-1 billion之间:

        1  0  0  0  0  0  0  0  0  0

       billion        million      thousand hundred   

                  |-----------|          |-----------|        |------------|

观察可以发现:每3个0更换一个单位。

我们可以设置一个unit 的hash map,存储单位,注意0最好也存上相对应的“”

再对1-9, 10-19, 20, 30, 40, 50, 60, 70 ,80, 90进行存储

对num进行模1000操作取出当前后3位,对后三位进行数文转换后根据当前技术值count加后面的单位。

需要注意的是:

  什么时候插入空格!

代码:

class Solution {
public:
    string numberToWords(int num) {
        unordered_map<int,string> dict ={
            {1, "One"}, {2, "Two"}, {3, "Three"}, {4, "Four"}, {5, "Five"}, {6, "Six"}, {7, "Seven"}, {8, "Eight"}, {9, "Nine"},
            {10, "Ten"},{11, "Eleven"}, {12, "Twelve"}, {13, "Thirteen"}, {14, "Fourteen"}, {15, "Fifteen"},{16, "Sixteen"}, 
            {17, "Seventeen"}, {18, "Eighteen"}, {19, "Nineteen"}, {20, "Twenty"}, {30, "Thirty"}, {40, "Forty"}, {50, "Fifty"},
            {60, "Sixty"}, {70, "Seventy"}, {80, "Eighty"}, {90, "Ninety"}
        };
        unordered_map<int,string> unit = {{0, ""},{1, " Thousand"}, {2, " Million"}, {3," Billion"}};
        string ret = "";
        int count = 0;
        if(num == 0)
            return "Zero";
        while(num > 0){
            int cur = num % 1000;
            if(cur != 0){
                string wn = toWords(cur, dict);
                wn += unit[count];
                if(ret.size() != 0){
                    ret = " "+ret;
                }
                ret = wn + ret;
            }
            count++;
            num /= 1000;
        }
        return ret;
        
    }
private:
    string toWords(int n, unordered_map<int, string> &m){
        string ret;
        int h = n/100;
        if(h != 0)
            ret = m[h] + " Hundred";
        n %= 100;
        if(n == 0)
            return ret;
        if(m.count(n)){
            if(ret.size() != 0)
                ret += " ";
            ret += m[n];
        }else{
            int t = n/10;
            if(t != 0){
                if(ret.size() != 0)
                    ret += " ";
                ret += m[t*10];
            }
            int d = n%10;
            if(d != 0){
                if(ret.size() != 0)
                    ret += " ";
                ret += m[d];
            }
        }
        return ret;
    }
};

 

273. Integer to English Words

标签:行存储   color   represent   ber   vat   for   之间   count   turn   

原文地址:https://www.cnblogs.com/yaoyudadudu/p/9191972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!