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. HR外包系统 - 工资计算-几种常见账单计算规则

    01-正常工资计税 (包括同一月多地计税方式) 02-年终奖计税 (包括可分批发放,但计税总额不变)  按工资 除以月份,看落在那个计税区间,获取税率和扣除数,再用总额*税率-扣除数,要考虑当月工资如 ...

  2. 【使用Unity开发Windows Phone上的2D游戏】(1)千里之行始于足下

    写在前面的 其实这个名字起得不太欠当,Unity本身是很强大的工具,可以部署到很多个平台,而不仅仅是可以开发Windows Phone上的游戏. 只不过本人是Windows Phone 应用开发出身, ...

  3. Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads 并查集+优先队列

    D. Fools and Foolproof Roads   You must have heard all about the Foolland on your Geography lessons. ...

  4. AngularJS学习之HTML DOM

    1.AngularJS为HTML DOM元素的属性提供了绑定应用数据的指令: 2.ng-disabled指令:直接绑定应用程序数据到HTML的disable属性: <div ng-app=&qu ...

  5. SPOJ REPEATS 后缀数组

    题目链接:http://www.spoj.com/problems/REPEATS/en/ 题意:首先定义了一个字符串的重复度.即一个字符串由一个子串重复k次构成.那么最大的k即是该字符串的重复度.现 ...

  6. 《DSP using MATLAB》示例Example4.15

    代码: b = [1/3, 1/3, 1/3]; a = [1, -0.95, 0.9025]; % x(n) y(n) coefficient [R, p, C] = residuez(b,a) M ...

  7. express-16 与生产相关的问题2

    处理未捕获的异常 在Node的异步世界中,未捕获的异常是特别需要关注的问题 app.get('/fail', function(req, res){ throw new Error('Nope!'); ...

  8. configSections

         由于最近一个项目的数据库变动比较频繁, 为了减少数据层的负担, 打算采用.net的MVC框架, 使用LINQ对付数据层.       这个框架的web.config文件里出现了configS ...

  9. js小例子(标签页)

    运用js写的一个小例子,实现点击不同的标签出现不同的内容: <!DOCTYPE html> <html> <head> <meta chaset=" ...

  10. 后缀数组 POJ 1743 Musical Theme

    题目链接 题意:给定n个数字,求超过5个数字的,最长的,变化相同的,不相交的重复子串 分析:男人8题中的一题!数列相邻两项做差,形成新数列,即求数列中的最长重复子串(不可相交). 后缀数组+二分答案. ...