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. 发送http请求get方法

    //获取网页html NSURL* url = [NSURL URLWithString:@"http://www.baidu.com"]; NSMutableURLRequest ...

  2. dwz中权限的控制

    很多人不明白用dwz要如何在没有登录的时候跳转到登录页面,没有权限的时候弹出提示. 其实,作者在设计的时候,已经完全考虑到了这些需求. 不管是navTab还是dialog,dwz的页面加载最终都是通过 ...

  3. 关于SQLSERVER联合查询一点看法

    首先看一段代码 这个数据库表我就不发了,这段代码的意思是:查询Book表中大于该类图书价格平均值的图书信息, 先看()里的内容,我一个表起了两个别名,让这个表的相同的id相等,查出平均分,然后再看() ...

  4. AndroidStudio中创建Assets文件

  5. supervisor(二)event

    supervisor的event机制其实,就是一个监控/通知的框架.抛开这个机制实现的过程来说的话,event其实就是一串数据,这串数据里面有head和body两部分.咱们先弄清楚event数据结构, ...

  6. MySQL的多表查询(笛卡尔积原理)

    先确定数据要用到哪些表. 将多个表先通过笛卡尔积变成一个表. 然后去除不符合逻辑的数据(根据两个表的关系去掉). 最后当做是一个虚拟表一样来加上条件即可. 注意:列名最好使用表别名来区别. 笛卡尔积 ...

  7. Android MVP理解

    Android默认采用的是MVC: View:对应于布局文件 Model:业务逻辑和实体模型 Controllor:对应于Activity 但是却存在很多问题: 1.这个View对应于布局文件,其实能 ...

  8. JDK NIO编程

    我们首先需要澄清一个概念:NIO到底是什么的简称?有人称之为New I/O,因为它相对于之前的I/O类库是新增的,所以被称为New I/O,这是它的官方叫法.但是,由于之前老的I/O类库是阻塞I/O, ...

  9. 运用Python成为黑客

    1.Fuzz测试: 漏洞挖掘有三种方法:白盒代码审计.灰盒逆向工程.黑盒测试.其中黑盒的Fuzz测试是效率最高的一种,能够快速验证大量潜在的安全威胁. Fuzz测试,也叫做"模糊测试&quo ...

  10. Ubuntu mysql

    To install mysql database in the ubuntu:    1. sudo apt-get install mysql-server 2. apt-get isntall ...