题目描述
小明有一架天平,小明想称出1∼n 克的物品,请问最少需要几颗砝码?
比如小明想称出1∼4 克的物品,需要2颗砝码,为1和3克。
balance
输入
第一行是一个整数T(1≤T≤10000) ,表示样例的个数。 以后每行一个样例,为一个整数 (1≤n≤10 9 )。
输出
每行输出一个样例的结果。
样例输入
3
1
4
40
样例输出
1
2
4
思路:
这道题第一感觉告诉我们是道规律题(可能是因为数据非常大的关系,用dp也没办法表示状态,太大了),于是我们去手动模拟答案,可以发现对于
n为1 2 3 4 5 6 7 8 9 10 11 12 13 是使用的砝码数分别是 1 2 2 2 3 3 3 3 3 3 3 3 3,并且使用的砝码重量为1,3,9,这样的话我们可以尝试认为用1,3,9,27的砝码可以测n<=40的情况,发现大部分都是可以的,那么尝试写一发吧,发现过了
其实是可以用数学归纳法证明的,但是我这种数学渣不会啊
ac代码
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<map>
#include<sstream>
#include<cstring>
#define LL long long
using namespace std;
int main()
{
LL n;
while(~scanf("%lld",&n))
{
LL now = 1;
LL ans = 0;
LL sum = 0;
while(sum<n)
{
ans++;
sum+=now;
now*=3;
}
cout<<ans<<endl;
}
}