博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Integer to English Words
阅读量:7059 次
发布时间:2019-06-28

本文共 2902 字,大约阅读时间需要 9 分钟。

题目: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
numsb20 = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; vector
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;}

 

转载于:https://www.cnblogs.com/yeqluofwupheng/p/6821953.html

你可能感兴趣的文章
Automatic Truncation of Virtual Log Files(VLFs的自动截断)
查看>>
[Z]寻找第K大的数的方法总结
查看>>
javascript一些常用代码块
查看>>
利用EntityFramework获得双色球数据库
查看>>
IOS4.0与5.0解决键盘的冲突
查看>>
mongo-mapreduce测试(10)——阶段总结(2)
查看>>
Setting an Oracle event:The structure of the trace syntax
查看>>
CSS+DIV:父DIV相对定位+子DIV绝对定位
查看>>
DBCC--SHRINKDATABASE
查看>>
我的开源路声明
查看>>
C# 图像处理:获取鼠标位置信息(全局)
查看>>
angular学习笔记(一)-入门案例
查看>>
jQuery hover事件鼠标滑过图片半透明标题文字滑动显示隐藏
查看>>
BackTrack5 (BT5)无线password破解教程之WPA/WPA2-PSK型无线password破解
查看>>
jQuery修改class属性和CSS样式
查看>>
Web.xml配置具体解释之context-param
查看>>
Qt Widgets——抽象按钮及其继承类
查看>>
svn 版本管理与自动部分发布(转)
查看>>
php 上传图片
查看>>
Lambda表达式详解
查看>>