链接:https://ac.nowcoder.com/acm/contest/327/C
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
处女座热爱做物理实验,为了实验,处女座必须要精确的知道物品的质量。处女座准备自己设计一套砝码,每一个砝码都是正整数,这套砝码必须能够精确测量出n以内所有正整数的质量,处女座想要知道至少需要多少个砝码。你可以在天平的任意一边放置砝码。
输入描述:
一行,一个正整数n
1<=n<=101000
输出描述:
一个整数,表示最少的砝码数。
示例1
输入
复制
20
输出
复制
4
说明
你可以选择1,2,6,11
1=1
2=2
3=1+2
4=6-2
5=6-1
6=6
7=6+1
8=6+2
9=6+2+1
10=11-1
11=11
12=11+1
13=11+2
14=11+2+1
15=11+6-2
16=11+6-1
17=11+6
18=11+6+1
19=11+6+2
20=11+6+2+1
使用 1 3 9 27 找规律
发现
1之后的 1种
1+3之后的 2种
1+3+9之后的 3种
。。。
import java.util.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger n, ans;
cin.hasNext();
n = cin.nextBigInteger();
BigInteger st = BigInteger.valueOf(3);
ans = BigInteger.ONE;
BigInteger f = BigInteger.ONE;
BigInteger sum = BigInteger.ONE;
while (f.compareTo(n) < 0) {
sum = sum.multiply(st);
f = f.add(sum);
ans = ans.add(BigInteger.ONE);
}
System.out.println(ans);
}
}