- 问题描述
There are 1000 buckets, one and only one of them is poisonous, while the rest are filled with water. They all look identical. If a pig drinks the poison it will die within 15 minutes. What is the minimum amount of pigs you need to figure out which bucket is poisonous within one hour?
Answer this question, and write an algorithm for the general case.
- 解决方案
类似于键盘扫描确定哪个按键被按下的思想。代码如下:
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
int base = minutesToTest / minutesToDie + 1;
double res = Math.log(buckets) / Math.log(base);
return (int)Math.ceil(res);
}