数字发音
来源:互联网 发布:淘宝外围活动是什么 编辑:程序博客网 时间:2024/04/28 02:25
题目描述
有一个非负整数,请编写一个算法,打印该整数的英文描述。
给定一个int x,请返回一个string,为该整数的英文描述。
测试样例:
1234
返回:"One Thousand,Two Hundred Thirty Four"
class ToString{public: string toString(int x) { // write code here string base[20] = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; //0~19 string tyNum[10] = {"","","Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty","Ninety"}; string bigNum[4] = {"", "Thousand", "Million", "Billion"}; string result; int k=0; //将x每三位分成一组进行编码 if(x==0) return "Zero"; while(x>0) { int val=x%1000; //对三位数进行编码 string temp; if(base[val/100]!="") //百位数 { temp+=base[val/100]; temp+=" Hundred"; } if(val%100<20) { if(!temp.empty()) temp+=" "; temp+=base[val%100]; } else { if(!temp.empty()) temp+=" "; temp+=tyNum[val%100/10]; if(base[val%10]!="") { if(!temp.empty()) temp+=" "; temp+=base[val%10]; } } if(!temp.empty()&&bigNum[k]!="") { temp+=" "; temp+=bigNum[k]; if(!result.empty()) temp+=","; } result=temp+result; k++; x/=1000; } return result; }};
0 0
- 数字发音
- 数字发音
- 数字发音
- 关于数字0的发音
- 面试题: 数字转英文 数字发音
- 《Cracking the Coding Interview程序员面试金典》----数字发音
- SQL发音
- 希腊字母发音
- 发音校正
- 术语发音
- 中英文发音
- 希腊语发音
- 英语发音
- C#发音
- 日语发音
- 罗马字母发音
- 韩语发音
- 鼻音发音
- EditText设置光标的位置和字符选择
- Android -- ConnectifyService网络更新流程简介
- QT系列教程
- Android Error:Execution failed for task ':app:transformClassesWithDexForDebug'.异常
- 取n个数中第k大数
- 数字发音
- 数据挖掘标准流程
- C语言中nan的使用
- 基于粒子群算法的概率神经网络实现多分类(PSO_PNN)
- JBoss下面部署应用的Log4j日志不能显示问题
- C#的 第6节课
- 39. Combination Sum
- JavaScript获取DOM元素位置和尺寸大小
- centos下espeak文本转语音的代码实现