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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. 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 ...

  7. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. 《Cracking the Coding Interview》——第17章:普通题——题目14

    2014-04-29 00:20 题目:给定一个长字符串,和一个词典.如果允许你将长串分割成若干个片段,可能会存在某些片段在词典里查不到,有些则查得到.请设计算法进行分词,使得查不到的片段个数最少. ...

  10. 《Cracking the Coding Interview》——第17章:普通题——题目13

    2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...

随机推荐

  1. 验证tensorflow版本是GPU还是CPU

    reference: https://blog.csdn.net/zlase/article/details/79261348 import numpy import tensorflow as tf ...

  2. 永恒之蓝EternalBlue复现

    0x01 漏洞原理:http://blogs.360.cn/blog/nsa-eternalblue-smb/ 目前已知受影响的 Windows 版本包括但不限于:Windows NT,Windows ...

  3. 搭建TFTP服务器配置

    实验内容: TFTP是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂,开销不大的文件传输服务.TFTP承载在UDP上,提供不可靠的数据传输服务,不提供存取授权与认 ...

  4. hadoop中compare函数

    在看hadoop  的二次排序的时候,改写了下, 加了第三个参数,  本来以为是在 public int compareTo(IntPair o) { System.out.println(" ...

  5. 继承FileInputFormat类来理解 FileInputFormat类

    import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.had ...

  6. C#中this保留字的用法

    一.this保留字 this保留字一般只在构造函数.类的方法和类的实例中使用.它有以下含义: ?在类的构造函数中出现的this,则作为一个值类型,表示对正在构造的对象本身的引用. ?在类的方法中出现的 ...

  7. django中csrftoken跨站请求伪造的几种方式

    1.介绍 我们之前从前端给后端发送数据的时候,一直都是把setting中中间件里的的csrftoken这条给注释掉,其实这个主要起了一个对保护作用,以免恶意性数据的攻击.但是这样直接注释掉并不是理智型 ...

  8. mybatis中oracle转mysql

    刚来公司实习,遇到的第一个任务就是这个,简单记录一下思路过程.人菜的很,没啥参考价值. 测试时: 将现有的oracle库转为mysql: 用的Navicat自带数据传输功能,简单粗暴 出现的问题: 1 ...

  9. linux下csv导出文件中文乱码问题

    近日在服务器端通过导出csv文件,将数据从linux服务器端保存到windows桌面端,以便用户可以通过excel打开使用数据. 但是在使用excel打开csv文件时,出现了中文乱码的情况,但是使用记 ...

  10. python__基础 : 类属性,类方法,静态方法

    类属性  定义在类里面,方法外面的属性,一般属于这个类,如下面的 num 就是类属性: class Test: num = 类属性用 实例.类属性   或者 类.类属性 都可以访问, 如 a = Te ...