《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 ...
随机推荐
- vue - 绑定css、style
1.绑定html css1.1对象语法: 传给 v-bind:class 一个对象,以动态地切换 class <div v-bind:class="{ active: isActive ...
- graphql 项目搭建(二)
一.Express基本框架 1.新建一个文件夹gql-server vscode 打开项目,在终端下输入yarn init -y 生成package.json 包(如果没安装yarn ,npm也一样, ...
- 运行在 Android 系统上的完整 Linux -- Termux
Termux 可以在安卓系统上搭建一个完整的linux 环境,类似于 cygwin 并非linux 虚拟机,整个安装包只有 几百KB 刚开始觉得这东西的命令行很难用,看了官方介绍后才发现它原来有许多 ...
- 运行npm update等命令出错后如何分析问题根源
我今天工作时,在当前前端项目工作目录下执行命令npm update 结果遇到如下错误:registry error parsing json npm ERR! Unexpected token < ...
- S/4HANA for Customer Management里的搜索分页处理
这篇文章的英文版我发在了SAP Community上:Paging Implementation in S/4HANA for Customer Management https://blogs.sa ...
- 编译安装 mysql 5.5,运行 cmake报错Curses library not found
是因为 curses库没有安装,执行下面的语句即可 yum -y install ncurses-devel 如果上述命令的结果是no package,则使用下面的命令安装 apt-get insta ...
- cudaMallocPitch – 向GPU分配存储器
概要 cudaError_t cudaMallocPitch( void** devPtr,size_t* pitch,size_t widthInBytes,size_t height ) 说明 向 ...
- 共变导数(Covariant Derivative)
原文链接 导数是指某一点的导数表示了某点上指定函数的变化率. 比如,要确定某物体的速度在某时刻的加速度,就取时间轴上下一时刻的一个微小增量,然后考察速度的增量和时间增量的比值.如果这个比值比较大,说明 ...
- matlab linux下无界面运行
今日做吸引域的仿真,由于需要遍历100*100*100的空间,需要的时间比较长,发现程序没运行一段时间,就会出现Out of memory的错误,而且出错的部分在于截取figure内部图片的部分. 开 ...
- jeDate日期控件
http://www.jayui.com/jedate/ 这是日期控件官网,可以去里面下载使用 前台 <%@ Page Language="C#" AutoEvent ...