17.7 Given any integer, print an English phrase that describes the integer (e.g., "One Thousand, Two Hundred Thirty Four").

LeetCode上的原题,请参见我之前的博客Integer to English Words

string convert_hundred(int num) {
vector<string> v1{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
vector<string> v2{"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eigthy", "Ninety"};
string res;
int a = num / , b = num % , c = num % ;
res = b < ? v1[b] : v2[b / ] + (c ? " " + v1[c] : "");
if (a > ) res = v1[a] + " Hundred" + (b ? " " + res : "");
return res;
} string num_to_string(int num) {
if (num < ) return "Negative " + num_to_string(- * num);
vector<string> v = {"Thousand", "Million", "Billion"};
string res = convert_hundred(num % );
for (int i = ; i < ; ++i) {
num /= ;
res = num % ? convert_hundred(num % ) + " " + v[i] + " " + res : res;
}
while (res.back() == ' ') res.pop_back();
return res;
}

CareerCup All in One 题目汇总

[CareerCup] 17.7 English Phrase Describe Integer 英文单词表示数字的更多相关文章

  1. [LeetCode] 273. Integer to English Words 整数转为英文单词

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

  2. [LeetCode] Integer to English Words 整数转为英文单词

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

  3. [leetcode]273. Integer to English Words 整数转英文单词

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

  4. [CareerCup] 17.14 Unconcatenate Words 断词

    17.14 Oh, no! You have just completed a lengthy document when you have an unfortunate Find/Replace m ...

  5. [CareerCup] 17.10 Encode XML 编码XML

    17.10 Since XML is very verbose, you are given a way of encoding it where each tag gets mapped to a ...

  6. CareerCup: 17.14 minimize unrecognized characters

    Oh, no! You have just completed a lengthy document when you have an unfortu- nate Find/Replace misha ...

  7. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  8. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

  9. [CareerCup] 17.12 Sum to Specific Value 和为特定数

    17.12 Design an algorithm to find all pairs of integers within an array which sum to a specified val ...

随机推荐

  1. Android adb的使用

    参考:http://blog.csdn.net/veryitman/article/details/6437090 1. 进入shell 进入设备shell adb shell 2. 安装 apk & ...

  2. pythonchallenge之C++学习篇-01

    字符处理时每个语言都具备的一种功能,其中还有一些语言因此出名,比如perl,shell,还有一些函数式的编程语言 C语言中的字符串与数组和指针联系的比较紧密,因此可以这样生命字符串*p="h ...

  3. ubuntu14使用qemu调试linux内核

    # 下载内核源代码编译内核 cd ~/LinuxKernel/ wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.6.tar.x ...

  4. telnet时显示:允许更多到 telnet 服务器的连接。请稍候再试

    telnet时显示:允许更多到 telnet 服务器的连接.请稍候再试    解决办法: windows自带telnet服务器默认的最大连接数为2,要想修改该设置,可以在命令行键入tlntadmn c ...

  5. CSS3动画属性animation的用法

    转载: 赞生博客 高端订制web开发工作组 » CSS3动画属性animation的用法 CSS3提供了一个令人心动的动画属性:animation,尽管利用animation做出来的动画没有flash ...

  6. 关于css的全面学习笔记

    1.text-align 属性规定元素中的文本的水平对齐方式.该属性通过指定行框与哪个点对齐,从而设置块级元素内文本的水平对齐方式.通过允许用户代理调整行内容中字母和字之间的间隔,可以支持值 just ...

  7. JQuery学习之遍历

    1.祖先:向上遍历DOM树 **parent():返回被选中元素的直接父元素,该方法只会向上一级对DOM树进行遍历 $(document).ready(function(){ $("span ...

  8. 实现点击不同的按钮加载不同的css

    这段时间做一个小网站发现有时候特别需要点击不同的按钮去加载不同的css,这样可以确定点击的是哪个,由于每个按钮都是从后端数据库加载过来的,不仅是简简单单的用id或者是类名,用过this也不行: 前端加 ...

  9. Jquery和JS获取ul中li标签

    js 获取元素下面所有的li var content=document.getElementById("content"); var items=content.getElemen ...

  10. ccc 音乐播放

    cc.Class({ extends: cc.Component, properties: { musicPlayer: { default: null, type: cc.AudioSource } ...