• [LeetCode]Integer to English Words


    题目: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"

    将数字转为英文表示。

    思路:

    主要需要分情况讨论:设数字为n

    1.当n < 20时,可以直接使用一个单词表示;

      vector<string>numsb20 = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };

    2.当20 <= n < 100时,需要几十的表示法,

      vector<string>numsb100 = { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };

    3.当100 <= n < 1000时,需要百的表示法;

    4.当1000 <= n < 1000000000时,需要千、百万、十亿的表示法,

      他们每个间隔1000,其中包括上面的三种情况的表示法,注意int的范围使超过十亿的位数的数值不会超过20;

    5.0特殊处理。

    string LeetCode::numberToWords(int num){
        if (!num)return "Zero";
        string str;
        vector<string>numsb20 = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
        vector<string>numsb100 = { "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety" };
        while (num){
            if (num >= 1000000000){//十亿
                int i = num / 1000000000;//int范围使i最大值不可能超过20
                num %= 1000000000;
                str += numsb20.at(i - 1) + " Billion ";
            }
            else if (num >= 1000000){//百万
                int i = num / 1000000;
                num %= 1000000;
                if (i >= 100){//i超过一百
                    str += numsb20.at(i / 100 - 1) + " Hundred ";
                    i %= 100;
                }
                if (i >= 20){//i需要使用20以上的数表示
                    str += numsb100.at(i / 10 - 2) + " ";
                    i %= 10;
                }
                if(i)str += numsb20.at(i - 1) + " Million ";
                else str += "Million ";
            }
            else if (num >= 1000){//一千
                int i = num / 1000;
                num %= 1000;
                if (i >= 100){
                    str += numsb20.at(i / 100 - 1) + " Hundred ";
                    i %= 100;
                }
                if (i >= 20){
                    str += numsb100.at(i / 10 - 2) + " ";
                    i %= 10;
                }
                if(i)str += numsb20.at(i - 1) + " Thousand ";
                else str += "Thousand ";
            }
            else if (num >= 100){//一百
                int i = num / 100;
                num %= 100;
                str += numsb20.at(i - 1) + " Hundred ";
            }
            else if (num >= 20){//几十
                int i = num / 10 - 2;
                num %= 10;
                str += numsb100.at(i) + " ";
            }
            else if (num >= 1){//个位或十几
                str += numsb20.at(num - 1) + " ";
                num = 0;
            }
        }
        str.pop_back();
        return str;
    }
  • 相关阅读:
    如何学习Java基础
    什么阻碍手动测试发挥价值
    API自动化测试指南
    筛选自动化测试用例的技巧
    拷贝HttpRequestBase对象
    测试自动化的边缘DevTestOps
    2019年浏览器市场份额排行榜
    CentOS7 通过 YUM 升级 VIM8
    vim配置go语法高亮
    打开程序总是会提示 "Enter password to unlock your login keyring"
  • 原文地址:https://www.cnblogs.com/yeqluofwupheng/p/6821953.html
Copyright © 2020-2023  润新知