题目:

总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐?

分析:

为什么可怜不言而喻...本题可以这么考虑问题, 先是二维地排列罐子, 然后分别让两头猪去尝试找出哪一行和哪一列有毒.间隔时间为15分钟, 由于测试时间是60分钟 所以总共1只猪能测试5行或者5列. (这里不是4行或者4列, 因为如果前面4个测试猪都没死的话, 说明最后一行/最后一列肯定有毒). 总结一下,1个维度交给1只猪, 它鞠躬尽瘁死而后已, 能帮我们检查出(测试时间/毒发时间 + 1)个维度单位.

那么回到二维的例子里面, 2只猪能帮我们检查5*5=25个罐子,那么如果是三维, 就是5^3 = 125个, 以此类推随着维度的上升,只要最后的水桶数大于我们需要检查的水桶数,就需要几头猪.

1   2   3   4   5

6   7   8   9  10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

代码:(相对简单)

public class solution{

  public int poorPigs(int buckets, int timeToDie, int timeToTest) {

    int pigs = 0;

    while (Math.pow((timeToTest / timeToDie + 1), pigs) < buckets) {

      pig++;

    }

    return pigs;

  }

}

Leetcode - 458 Poor Pigs的更多相关文章

  1. 【LeetCode】458. Poor Pigs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. [LeetCode&Python] Problem 458. Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  3. [LeetCode] 458. Poor Pigs_Easy tag: Math

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  4. 458. Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  5. 1228.Poor Pigs 可怜的猪

    转自[LeetCode] Poor Pigs 可怜的猪 There are 1000 buckets, one and only one of them contains poison, the re ...

  6. Leetcode: Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  7. [LeetCode] Poor Pigs 可怜的猪

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  8. LeetCode算法题-Poor Pigs(Java实现)

    这是悦乐书的第235次更新,第248篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第102题(顺位题号是455).有1000个水桶,其中只有一个水桶含有毒药,其余的都没毒 ...

  9. [Swift]LeetCode458. 可怜的小猪 | Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

随机推荐

  1. Hadoop Streaming框架学习(一)

    Hadoop Streaming框架学习(一) Hadoop Streaming框架学习(一) 2013-08-19 12:32 by ATP_, 473 阅读, 3 评论, 收藏, 编辑 1.Had ...

  2. MySQL 常用命令大全2

    下面贴出我在实际工作中遇到mysql操作数据表的sql命令,如有不对的地方,请多指教: c++链接mysql头文件命令 g++ is_in_polygon.cpp -o is_in_polygon - ...

  3. KingPaper初探 wamp下本地虚拟主机的搭建

    在本地我们进行网站或系统开发时,因为我们本地的地址以localhost为主机名的  我们上传到服务器会有很多东西要修改 为了避免这些不必要的修改,我们可以在本地搭建虚拟主机 一下是在wamp下搭建虚拟 ...

  4. 读excel时候出现java内存溢出

    修改Eclipse,或MyEclipse的内存 例如MyEclipse 在window->preferences->myeclipse->application server-> ...

  5. 根据Mob官网的天气预报接口写了一个简单的demo

    第一步 自己注册一个应用,然后获取里面的 App Key,下载MobAPI SDK 然后拖入 MobAPI.framework 和 MOBFoundation.framework 到你的项目中 第二步 ...

  6. EL表达式学习

    EL表达式取值 1.EL表达式的语法格式很简单: 以前编写jsp代码时,如果要获取表单中的用户名,一般使用 <%=request.getParameter("name")%& ...

  7. SSH三大框架的基本整合以及常见错误的解决方法

    一.新建项目 eclipse->file->new->other->Dynamic Web Project,project name为sshDemo 二.下载jar包 1.st ...

  8. Python学习--10 面向对象编程

    面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 本节对于面向对象的概念不做 ...

  9. CodeForces 721D Maxim and Array

    贪心,优先队列. 先看一下输入的数组乘积是正的还是负的. ①如果是负的,也就是接下来的操作肯定是让正的加大,负的减小.每次寻找一个绝对值最小的数操作就可以了. ②如果是正的,也是考虑绝对值,先操作绝对 ...

  10. mongo数据库时间存储的问题

    题记:项目中要加的内容,可以实现对设备的预定,被某个用户预定后的设备就不能再被其他用户所使用了,用户预定的时候就需要输入预定时间,web前端用到了boostrap的date的一个插件,非常好用,接下来 ...