每个输入包含一个测试用例。
输入的第一行包括一个正整数,表示一开始小人的数量N(1<=N<=10^100)。
对于每个用例,在单独的一行中输出牛牛需要打多少次响指才能让小人的数量变成1。
10000
20
import java.util.Scanner;
import java.math.BigInteger;
public class Main {
// public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// BigInteger bi = new BigInteger(sc.nextLine());
// int count = 0;
// BigInteger two = new BigInteger("2");
// while (!bi.equals(BigInteger.ONE)) {
// if (bi.remainder(two).equals(BigInteger.ZERO)) {
// bi = bi.divide(two);
// } else {
// bi = bi.add(BigInteger.ONE);
// }
// count++;
// }
// System.out.println(count);
// }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String str = sc.next();
String bin = new BigInteger(str).toString(2);
int len = bin.length();
int count = 0;
for (int i = len - 1; i >= 0; i--) {
if (bin.charAt(i) == '1') {
for (int j = 0; j < i; j++) {
if (bin.charAt(j) == '0') {
count++;
}
}
break;
}
}
if (bin.length() == 1) {
System.out.println(0);
} else {
System.out.println(len + count + 1);
}
}
}
}
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger bi= new BigInteger(sc.nextLine());
System.out.println(times(bi));
}
private static int times(BigInteger num) {
int times= 0;
BigInteger t = new BigInteger("2");
while (!num.equals(BigInteger.ONE)) {
if (num.remainder(t).equals(BigInteger.ZERO)) {
num=num.divide(t);
}
else {
num=num.add(BigInteger.ONE);
}
times++;
}
return times;
}
}