1000瓶水,其中有一瓶有毒,小白鼠喝后24小时会死忙。请问最少多少只老鼠,可以在24小时测出哪瓶有毒。
答案:最少需要[log21000]=10只小白鼠

试验方法:

1000瓶水,分别编号从1到1000,并用10位二进制表示。
10只老鼠,编号从0到9。
喝药策略:
对于编号为X的水, 二进制表示为b9b8b7b6b5b4b3b2b1b0。如果bi=1(i=0,…,9),则表示第i个小老鼠要喝这瓶水;否则,不喝。
如:ID=7的水,二进制表示为0000000111,则表示第0,1,2只老鼠要喝这瓶水,6-9小老鼠不喝。
结果:
假设编号为X的水为毒水(二进制表示为b9b8b7b6b5b4b3b2b1b0)。
如果第i个小白鼠活着,则说明第i个小白鼠没喝这瓶水,则bi=0;
同理,如果第i个小白鼠死了,则说明第i个小白鼠喝了这瓶毒水,则bi=1。
根据10只小白鼠的活(0)或死(1)的状态,我们就可以唯一确定毒水的编号。

Move Zeroes

Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. For exa...

阅读全文

Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not ...

阅读全文

Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space...

阅读全文