458.POOR PIGS 进制

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),或者其他任意条件呢?

记住进制的转换方式是都可以解的

 

posted @ 2017-05-29 20:44  LiebeCelery  阅读(135)  评论(0编辑  收藏  举报