《Cracking the Coding Interview》——第17章:普通题——题目7
2014-04-28 23:28
题目:给定一个数字,用英语把它读出来。
解法:ZOJ上有相反的题目。如果我要用中文读书来呢?
代码:
// 17.7 Read an integer in English.
#include <map>
#include <string>
using namespace std; map<int, string> m; void init()
{
m[] = "zero";
m[] = "one";
m[] = "two";
m[] = "three";
m[] = "four";
m[] = "five";
m[] = "six";
m[] = "seven";
m[] = "eight";
m[] = "nine";
m[] = "ten";
m[] = "eleven";
m[] = "twelve";
m[] = "thirteen";
m[] = "fourteen";
m[] = "fifteen";
m[] = "sixteen";
m[] = "seventeen";
m[] = "eighteen";
m[] = "nineteen";
m[] = "twenty";
m[] = "thirty";
m[] = "forty";
m[] = "fifty";
m[] = "sixty";
m[] = "seventy";
m[] = "eighty";
m[] = "ninety";
int i, j;
for (i = ; i <= ; ++i) {
for (j = ; j <= ; ++j) {
m[i * + j] = m[i * ] + "-" + m[j];
}
}
} void readNumber(int n)
{
if (n == ) {
return;
}
// here n is limited between [0, 999];
int a, b, c; a = n / ;
b = n % / ;
c = n % / ; if (a > ) {
printf("%s hundred ", m[a].c_str());
if (b != || c != ) {
printf("and ");
}
}
if (b * + c > ) {
printf("%s ", m[b * + c].c_str());
}
} int main()
{
init();
int n, n0; while (scanf("%d", &n) == ) {
if (n == ) {
printf("zero \n");
continue;
}
if (n < ) {
printf("minus ");
n = -n;
}
n0 = n;
if (n >= ) {
readNumber(n / );
printf("billion ");
n = n % ;
}
if (n >= ) {
readNumber(n / );
n = n % ;
printf("million ");
}
if (n >= ) {
readNumber(n / );
n = n % ;
printf("thousand ");
}
if (n0 >= && n / == ) {
printf("and ");
}
readNumber(n);
putchar('\n');
} return ;
}
《Cracking the Coding Interview》——第17章:普通题——题目7的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目14
2014-04-29 00:20 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...
- 《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...
随机推荐
- Windows 静态IP脚本
@echo off echo 快速设置IP地址和DNS为“静态” set 连接名称=以太网 set ip地址=192.168.1.80 set 子网掩码=255.255.255.0 set 网关地址= ...
- Cygwin Run in the Windows(Simulation of UNIX)
Preface Environment Cygwin Run in the Windows(Simulation of UNIX) Resource Cygwin Install:http://cyg ...
- golang实现文件上传权限验证(超简单)
Go语言创建web server非常简单,部署也很容易,不像IIS.Apache等那么重量级,需要各种依赖.配置.一些功能单一的web 服务,用Go语言开发特别适合.http文件上传下载服务,在很多地 ...
- Installing xgboost and cmake, mingw64 and mingw
Problem: installing the xgboost to get the python package for later importing
- Android(java)学习笔记65:Clock App 编写报错02
1. 首先之间看错误: 07-13 10:07:55.354: E/AndroidRuntime(8008): FATAL EXCEPTION: main 07-13 10:07:55.354: E/ ...
- python 爬poj.org的题目
主要是正则表达式不熟练,基础知识不扎实,函数也不怎么会用,下次再深入了解这3个函数吧. 主要是一个翻页的功能,其实,就是通过一个url替换一下数字,然后得到一个新的url,再找这个新的链接的信息. # ...
- CPP-STL:用vector保存对象时保存指针的优点, 以及reserve的使用(转)
代码1 #include <vector> #include <stdio.h> class A { public: A() { printf("A()/n" ...
- apache日志
<VirtualHost *:80> ServerAdmin chinasir.xyz@gmail.com DocumentRoot /var/www/html/baidu ServerN ...
- ajax实现分页页签
在一些搜索列表的页面中,我们会遇到一些需要处理页签的需求,一般这样的页面,要么是在JSP中处理,每次都跳页.这样做是个很方便的方法.但是如果页面上有很多和列表无关,每次都需要重新渲染是不是显得慢了一些 ...
- 第36章 SDIO—SD卡读写测试—零死角玩转STM32-F429系列
第36章 SDIO—SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...