SDUT OJ 周赛 找有毒的那杯水(思维逻辑 + 分治思想 )

本文介绍了一个趣味数学问题——如何通过最少数量的小白鼠找出有毒的水杯。通过对问题的逐步解析,最终给出了一种有效的算法实现,并附带源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

你打我啊

Time Limit: 500ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

最近看了一个非常好玩的题,如果有972杯水,其中有971个没有毒的,1个有毒的, 其中有毒的水的毒性需要一周才会发作,那么,我们最少需要多少只小白鼠才能够在一周的时间内确定那一杯水是有毒的。当然这种水题肯定难不住各位队员们。所 以我要出的题和上述的东西没有一点关系。
 
那既然上面的东西和这次的题没有什么关系那我为什么要说呢,很明显,我在凑字数,我实在是不知道该怎么凑了呀,他们说让我来一个又臭又长一看就不想做的题,还让我用英文写,所以嘛,我英语不好只能先来个中文废话了,Ok上正题吧。
 
Christmas was coming .Mr Smith had no money to buy any presents for his children .His wife was ill and he spent a lot of money on her medicine .
And the harvest was bad and all his family were going go be hungry the next spring .He was quite worried about it .
“We had only a cock ,”said Mrs Smith one day .“You’d better take it to the town .Sell it there and buy some cakes and sweets for our children .”
“It’s a good idea !”the man said and caught the cock the next morning and put it into a box .It was difficult to walk on the road covered with thick snow .
Two hours later he was very tied and wanted to have a rest .He put the box to the ground and sat down .
“The air in the box must be close ,”the man said to himself .“I’d better let the cock walk outside for a while ,or it’ll die .”
So he put the cock to the ground .When he started again ,he couldn’t catch it any longer .
“How foolish you are !”Mr Smith called out angrily .“You can herald the break of day at night but you cann’t find the way to the town in the daytime !”
 
Ok,It is a nonsense.Our question is what I mentioned in the first paragraph,if we have N cups of water.How many mice we need.

输入

 多组输入,每组输入一个N(1<=N<=2^32-1);

输出

 输出小白鼠的数目

示例输入

1000
972 

示例输出

10
10

算法分析:其实这道题和《找假硬币》一样的道理。
这有n杯水,让老鼠A品尝n/2,剩余的n/2 由B来喝!
如果老鼠A死了,说明有毒的水在A这边,否则在B那边,就按照这种思路一直把确定好的有毒水那一边的所有水半分给两只老鼠
喝,这样做最后绝对会确定出哪一杯水有毒!


#include <iostream>  
#include <string>  
#include <stdio.h>  
#include <algorithm>  
  
using namespace std;  
  
int MAX(int a, int b)  
{  
    return a>b?a:b;  
}  
  
int main()  
{  
    unsigned int n;  
    unsigned int cnt;  
    while(scanf("%d", &n)!=EOF)  
    {  
        cnt=0;  
        while(n!=0)  
        {  
            n=n/2;  
            cnt++;  
        }  
        cout<<cnt<<endl;  
    }  
    return 0;  
}  

 

转载于:https://www.cnblogs.com/yspworld/p/4140889.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值