2386: Find the Coin 一个天平,一堆石子中只有一个比较重,用最少的次数找出

这是一个关于找寻一堆硬币中唯一较重硬币的问题,通过最少的天平称重次数来确定哪一枚是重的。给定硬币数量N,需要求解找到这枚硬币的最小称重次数。案例中,Keenas解决了仅用两次称重就能找到的问题。

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

 2386: Find the Coin


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K213104Standard

One day, Φ has 8 golden coins. He knew one of them is heavier than others. He wants to find out it. Then he wants to borrow X’s scale. But X is so mean. He said Φ must pay $10 for every use. So Φ ask Keenas for help. After a while , Keenas work out that it just need two times to weight it.

After that, Φ and Keenas want to knew if they have N golden coins, how many times they should use the scale at least? Please help them.

 

Input

There are many cases.In every case,there is a integer N (0<=N<20000)

Output

Output the minmum number to use scale to find the coin.One case in a line.

Sample Input

0
1
2
8
9

Sample Output

0
0
1
2
2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[21000];
int main()
{
    f[2]=f[3]=1;
    for(int i=4;i<=20000;i++)
    {
        if(i%3==0) f[i]=f[i/3]+1;
        else if(i%3==1) f[i]=max(f[i/3]+1,f[i/3+1]+1);
        else f[i]=max(f[i/3+1]+1,f[i/3]+1);
    }
    int n;
    while(scanf("%d",&n)==1)
    {
        printf("%d/n",f[n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值