2014-04-29 00:00

题目:给定一个rand5()函数,能够返回0~4间的随机整数。要求实现rand7(),返回0~6之间的随机整数。该函数产生随机数必须概率相等。

解法:自己想了半天没想出等概率的方法,最后参考答案了。答案思想实在巧妙:随机0~24间的数,然后把21~24丢弃,剩余的0~20对7取模就是等概率随机数了。

代码:

 // 17.11 Given a rand5() method, which generates random integer between [0, 4]. Please implement rand7() for [0, 6].
// The key to this problem, is to make sure every number gets the same probability.
#include <cstdio>
#include <cstdlib>
using namespace std; int rand5()
{
return rand() % ;
} int rand7()
{
int val; while (true) {
val = * rand5() + rand5();
if (val < ) {
return val % ;
}
}
} int main()
{
while (true) {
printf("%d\n", rand7());
} return ;
}

《Cracking the Coding Interview》——第17章:普通题——题目11的更多相关文章

  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系列C#实现

    原版内容转自:CTCI面试系列——谷歌面试官经典作品 | 快课网 此系列为C#实现版本 谷歌面试官经典作品(CTCI)目录   1.1 判断一个字符串中的字符是否唯一 1.2 字符串翻转 1.3 去除 ...

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

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

随机推荐

  1. OpenGL学习 Our First OpenGL Program

    This shows you how to create the main window with the book’s application framework and how to render ...

  2. Second last week for the second last semester!

    This week, I focused more on the final project, such as H335(Computer structure, still confused with ...

  3. 修改hosts工具推荐SwitchHosts

    推荐一个修改hosts的工具.适合平时工作中经常修改hosts的开发测试. 下载地址:https://oldj.github.io/SwitchHosts/ 可以按各种环境或者项目添加,用的时候打开或 ...

  4. IOS http协议 总结

    HTTP协议1.面试题常见:聊一下HTTP协议(协议的完整的通信过程) ============================================================ 一.一 ...

  5. ACM Arabella Collegiate Programming Contest 2015 H. Capital City 边连通分量

    题目链接:http://codeforces.com/gym/100676/attachments 题意: 有 n 个点,m 条边,图中,边强连通分量之间可以直达,即距离为 0 ,找一个点当做首都,其 ...

  6. iterm2配置项

    1. 启动终端Terminal2. 进入当前用户的home目录    输入cd ~3. 创建.bash_profile    输入touch .bash_profile 在导入并应用完颜色方案之后,通 ...

  7. Java开发.gitignore文件包含.iml,.log的看法

    有一个开源项目https://github.com/github/gitignore 主要用来规范所有开发项目的.gitignore文件的编写,基本涵盖了所有的开发语言.开发环境等.今日我向JetBr ...

  8. JS动画与CSS3动画

    Js动画 show / hide var div = $('#test-show-hide'); div.show('slow'); // 在0.6秒钟内逐渐显示 div.hide(3000); // ...

  9. poj_1995_Raising Modulo Numbers

    Description People are different. Some secretly read magazines full of interesting girls' pictures, ...

  10. 22.1 高级函数【JavaScript高级程序设计第三版】

    函数是JavaScript 中最有趣的部分之一.它们本质上是十分简单和过程化的,但也可以是非常复杂和动态的.一些额外的功能可以通过使用闭包来实现.此外,由于所有的函数都是对象,所以使用函数指针非常简单 ...