《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,怎么扔才能减少最坏情况下的扔的次数? 解 ...
随机推荐
- Altium_Designer-怎么将“原理图的更改”更新到“pcb图”?
打开原理图,直击菜单栏>>Design,选择第一项,>>Update PCB Document...在弹出的对话框里面选择执行更改即可将原理图更新到工程下面对应的PCB.也可以 ...
- *205. Isomorphic Strings (string & map idea)
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- 814. Binary Tree Pruning(leetcode) (tree traverse)
https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...
- Android检查更新(是否强制更新)
Android应用客户端通常会需要更新,而且根据需求分为普通更新和强制更新.是否强制更新可通过检查更新时从服务器获取的标志位来判断. public class UpdateManager { priv ...
- C/C++语言代码规范
1.标识符名称: 标识符名称包括函数名.常量名.变量名等.这些名字应该能反映它所代表的实际东西,具有一定的意义,使其能 够见名知义,有助于对程序功能的理解.规则如下: 所有宏定义.枚举常数和const ...
- redis 对cmd的操作
这个是原子递增的知识点: 关于list部分: 利用lpush命令, rpush命令, lrange命令,对列表操作 此前 我已经 在列表(list)中 插入了 部分 元素了 关于集合set 部分 首先 ...
- HTTP 下载文件工具类
ResponseUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.File; im ...
- js动画之无缝滚动
效果图如下: HTML代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- Go Doc文档
Go为我们提供了快速生成文档和查看文档的工具,很容易编写查看代码文档.在项目协作过程中,可以帮助我们快速理解代码. 查看文档方式有两种:一种是通过终端查看,使用go doc命令,一种是通过网页查看,使 ...
- LeetCode977. 有序数组的平方
问题:977. 有序数组的平方 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序. 示例 1: 输入:[-4,-1,0,3,10] 输出:[0,1,9,1 ...