[CareerCup] 17.7 English Phrase Describe Integer 英文单词表示数字
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] 17.7 English Phrase Describe Integer 英文单词表示数字的更多相关文章
- [LeetCode] 273. Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [leetcode]273. Integer to English Words 整数转英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [CareerCup] 17.14 Unconcatenate Words 断词
17.14 Oh, no! You have just completed a lengthy document when you have an unfortunate Find/Replace m ...
- [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 ...
- CareerCup: 17.14 minimize unrecognized characters
Oh, no! You have just completed a lengthy document when you have an unfortu- nate Find/Replace misha ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- [CareerCup] 17.13 BiNode 双向节点
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...
- [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 ...
随机推荐
- 基于socket、多线程的客户端服务器端聊天程序
服务器端: using System; using System.Windows.Forms; using System.Net.Sockets; using System.Net;//IPAddre ...
- APP设计尺寸规范大全,APP界面设计新手教程【官方版】(转)
正值25学堂一周年之际,同时站长和APP设计同仁们在群里(APP界面设计 UI设计交流群,APP界面设计⑥群 APPUI设计③群58946771 APP设计资源⑤群 386032923欢迎大家加入交流 ...
- XML 文件解析
1.XML文件 <Data> <Movie id="1"> <title>good lucky to you</title> < ...
- MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践
主服务器上(注:应该是允许从机访问) GRANT REPLICATION SLAVE ON *.* to ‘rep1’@’192.168.10.131’ identified by ‘passwor ...
- C语言连接SQLSERVER数据库
第一步:配置ODBC.在配置ODBC时有用户DSN.系统DSN.和文件DSN三种方法,为了稳妥起见,采用系统DSN. DSN的名字叫LocalServer,帐号:sa,密码123456 第二步:打开V ...
- JavaScript设计模式——工厂模式
工厂模式:是一种实现“工厂”概念的面上对象设计模式.实质是定义一个创建对象的接口,但是让实现这个接口的类来决定实例化哪个类.工厂方法让类的实例化推迟到子类中进行.创建一个对象常常需要复杂的过程,所以不 ...
- 【HTML+CSS】七小时快速入门~~~~~~~
由于网络化的原因,学习很方便,但是也由于太方便了,学习资料很多会给刚想要入门却没有什么自制力的初学者造成困难,我自己来说学html和css先看了一本书,后来又辗转在慕课网.w3cschool等学习网站 ...
- 仓库、超市、服装、食品、批发零售手持打印PDA开单器-现场无线开单扫描 无线传输电脑
深圳浩瀚技是一家主要从事手持数据终端硬件.软件研究.销售服务为一体的高新企业公司.公司主要销售进销存等无线开单系统.工业级手持PDA,安卓数据采集器,RFID阅读器等设备.我们秉承“诚信.敏捷.繁荣” ...
- redis 的安装
1: redis 是什么 Redis is an open source (BSD licensed), in-memory data structure store, used as databas ...
- fzu月赛 2203 单纵大法好 二分
Accept: 8 Submit: 18Time Limit: 5000 mSec Memory Limit : 65536 KB Problem Description 人在做,天在看 ...