《Cracking the Coding Interview》——第6章:智力题——题目1
2024-09-06 18:47:44
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 ;
}
最新文章
- 【Java EE 学习 48】【Hibernate学习第五天】【抓取策略】【二级缓存】【HQL】
- Memcache笔记03-php操作Memcached
- Android Studio-AndroidStudio目录结构
- Matlab:max函数
- A javascript library providing cross-browser, cross-site messaging/method invocation. http://easyxdm.net
- 使用jxl.jar操作Excel
- wireshark设置抓服务器的包
- repeater一个简单的用法例子
- ArcGIS教程:创建特征
- iOS 力学动画生成器UIKit Dynamics 之碰撞效果解说
- iOS学习——iOS项目Project 和 Targets配置详解
- freemarker中的round、floor和ceiling数字的舍入处理(十七)
- Spring MVC的handlermapping之BeanNameUrlHandlerMapping初始化
- 从1....n中随机输出m个不重复的数
- liteos简介(一)
- 将python2代码转为python3
- 19. Remove Nth Node From End of List (JAVA)
- STL::sort函数实现
- Tomcat+Redis+Nginx实现session共享(Windows版)
- 用Visual C#创建Windows服务程序