《Cracking the Coding Interview》——第6章:智力题——题目1
2014-03-19 06:40
题目:有20瓶药,其中19瓶装的都是1.0克的药片,只有1瓶装了1.1克的药。给你一个能称出具体克数的电子秤,只允许你称一次,怎么找出那瓶不一样的?
解法:如果药片管够,从每个瓶子里取出数量各不相同的药片,根据质量的总和减去“期望的”质量总和,就知道哪瓶有问题了。
代码:
// 6.1 There are 20 bottles of pills, all of which have pills of 1g, except one with 1.1g.
// Given a balance that can provides exact measurement, can you devise a method to find out the special bottle with just one try?
// Answer:
// If you pick n[i] pills from bottle i, the total weight should be sigma(n[i] * 1.0).
// But there is one with 1.1g pills, that would cause a little deviation to the sum.
// For example, if we pick i pills from bottle i, the sum should be (20 + 1) * 20 / 2 = 210.
// The real weight w should be greater than 210, (w - 210) / (1.1 - 1.0) will be the number.
int main()
{
return ;
}
《Cracking the Coding Interview》——第6章:智力题——题目1的更多相关文章
- 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》——第6章:智力题——题目4
2014-03-20 01:02 题目:无力描述的一道智力题,真是货真价实的智力题,让我充分怀疑自己智力的智力题.有兴趣的还是看书去吧. 解法:能把题目看懂,你就完成80%了,用反证法吧. 代码: / ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第6章:智力题——题目6
2014-03-20 01:14 题目:有100栈灯,一开始都关着.如果你按照n从1~100的顺序,每次都掰一下n的倍数的开关(开->关,关->开),那么到最后有多少灯是亮的? 解法:这个 ...
- 《Cracking the Coding Interview》——第6章:智力题——题目5
2014-03-20 01:08 题目:扔鸡蛋问题.有一个鸡蛋,如果从N楼扔下去恰好会摔碎,低于N楼则不碎,可以继续扔.给你两个这样的鸡蛋,要求你一定得求出N,怎么扔才能减少最坏情况下的扔的次数? 解 ...
随机推荐
- make知识
makelist 语法 https://cmake.org/cmake/help/v3.10/manual/cmake-language.7.html CMakeLists.txt I am of t ...
- Java UUID Generator(JUG)
UG 是一个纯 Java 的 UUID 生成器. UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成UUID的API.UUID按照开放软件基金 会 (OS ...
- 富文本 文字图片点击,(TextView)
textview上的富文本支持 文字,图片的点击事件 - (void)protocolIsSelect:(BOOL)select { NSMutableAttributedString *attrib ...
- tarjan+topsort
题目 缩完点后统计入读为零的点就可以来. 因为缩完点后肯定是DAG #include<iostream> #include<cstdio> #include<algori ...
- 五、@property的参数
格式:@property(参数1,参数2)类型 名字: 参数可有可无 如:@property int age; @property (nonatomic,retain) UIButton* btn; ...
- C#中类的声明
一.C#中类的声明 在C#中必须先声明类,然后才能在程序中使用. 类的声明格式如下: [类的属性] [访问修饰符] class 类名称 [: 父类名]{ [成员修饰符] 类的成员变量或者成员函数 ...
- django中的forms组件(权限信息校验,增删改查)
1.用处 1.用户请求数据验证 2.自动生成错误信息 3.打包用户提交的正确信息 4.如果其中有一个错误了,其他的正确,则保留上次输入的内容 5.自动创建input标签并可以设置样式 6.基于form ...
- JS isArray、typeof、instanceof
Array.isArray() 用来检验是不是数组 var a = [1,2,3] console.log(typeof a); // object console.log(Array.isArray ...
- Servlet的工作原理和生命周期
Servlet的工作原理 . Web服务器加载Servlet:Web服务器启动后,它会根据每个工程的web.xml文件去查找该工程的Servlet,并且找到这些Servlet的Class文件所在的地址 ...
- DFS练习-HDU1010
题目来源:HDU1010 DFS的基本原则已经差不多了,但是一些技巧仍然比较难想,所以还是加强练习,然后总结一下. 还是先看题意 ,指定迷宫的长,宽以及走出迷宫的具体时间N,M,T. 其中(1 < ...