zoukankan      html  css  js  c++  java
  • LeetCode 273. Integer to English Words

    题目

    很简单的模拟题啦

    class Solution {
    public:
        
         string number1[20] = { "Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen" };
    string number3[10] = { "Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety" };
    string number4[10] = { "Hundred","","Thousand","Million","Billion" };
    
    string numberToWords2(int num)
    {
        string ans = "";
        int a = num / 100;
        num %= 100;
        int b = num / 10;
        num %= 10;
        int c = num;
        if (a != 0)
        {
            ans += number1[a];
            ans += " ";
            ans += number4[0];
            ans += " ";
        }
    
        if (b >= 2)
        {
            ans += number3[b - 2];
            ans += " ";
        }
        else if (b == 1)
        {
            ans += number1[10 + c];
            ans += " ";
            ans = ans.substr(0, ans.size() - 1);
            return ans;
        }
        
        if (c != 0)
        {
            ans += number1[c];
            ans += " ";
        }
    
        ans = ans.substr(0, ans.size() - 1);
        return ans;
    }
        
    string function(int num1,int num2,int pos)
    {
        string ans="";
        int x = num1 / num2;
        if (x != 0)
        {
            string y = numberToWords2(x);
            ans += y;
            ans += " ";
            if(number4[pos]!="")
            {
                ans += number4[pos];
                ans += " ";
            }
        }
        
        return ans;
    
    }
    
    string numberToWords(int num) {
    
        string ans = "";
        
        int x = 1000000000;
        for(int i=4;i>=1;i--)
        {
            ans += function(num,x,i);
            num %= x;
            x/=1000;
        }
       
        if (ans == "")
            ans = "Zero";
    
        if(ans[ans.size()-1]==' ')
        ans = ans.substr(0, ans.size() - 1);
        return ans;
    }
    
    };
    
  • 相关阅读:
    RocketMQ4.5.2在centos7的安装
    android 9.x 实现应用内更新安装
    android listview 禁止滚动
    Failed to resolve loader: less-loader
    yarn的 文件名、目录名或卷标语法不正确
    Interceptor无法用Autowired自动注入Bean
    STL文件格式研究
    在C#中用COM操作CAD
    AVEVA CSG 几何图形输出接口
    PDMS数据库快速索引查询
  • 原文地址:https://www.cnblogs.com/dacc123/p/12858136.html
Copyright © 2011-2022 走看看