给你一个非零整数,让你求这个数的n次方,每次相乘的结果可以在后面使用,求至少需要多少次乘。
第一行m表示有m(1<=m<=100)组测试数据;
每一组测试数据有一整数n(0<n<=100000000)
思路:二分法,奇数乘两次,偶数一次。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int m =sc.nextInt();
int[] n = new int [m];
int temp;
for (int i = 0; i < n.length; i++) {
n[i] = sc.nextInt();
}
for (int i = 0; i < n.length; i++) {
int count=0;
temp = n[i];
while (temp!=1){
if (temp%2==0) {
count++;
}else {
count+=2;
}
temp/=2;
}
System.out.println(count);
}
}
}