458. Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison it will die within 15 minutes. What is the minimum amount of pigs you need to figure out which bucket contains the poison within one hour.
Answer this question, and write an algorithm for the follow-up general case.
Follow-up:
If there are n buckets and a pig drinking poison will die within m minutes, how many pigs (x) you need to figure out the "poison" bucket within p minutes? There is exact one bucket with poison.
一个很有意思的题 。
说一下follow up 的大概思路。
有sum桶水,可以用猪找出sum桶水中其中的一桶毒水,猪在如果喝到毒水在t时间后会死亡,给你t的时间,需要你找出最少ans只猪。
用进制的思想解。
进制k的选取是 k=(sumt/t)+1; //在所给的总时间内按分时间段找出有多少种可能性(k)会发生
给个代码:
#include<bits/stdc++.h>
using namespace std;
int main( )
{
int sumt,t,k,sum,ans,i,flag;
scanf("%d%d%d",&sumt,&t,&sum);
k=(sumt/t)+1;
flag=1;
for(ans=1;ans<sum;ans++)
{
flag*=k;
if(flag>sum)
break;
}
printf("%d\n",ans);
return 0;
如果给定其他条件要你求猪的抗性(t),或者其他任意条件呢?
记住进制的转换方式是都可以解的
本文探讨了如何使用最少数量的猪,在限定时间内找出1000桶中唯一含有毒水的桶。通过进制思想,文章提供了一种算法解决这个问题,并进一步讨论了更一般的情况,即当桶的数量、猪中毒后的死亡时间以及限定时间变化时,如何计算所需猪的最小数量。
1084

被折叠的 条评论
为什么被折叠?



