算法训练 乘法次数

Lan
Lan
2020-03-11 / 0 评论 / 674 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2020年03月11日,已超过1500天没有更新,若内容或图片失效,请留言反馈。
资源限制
时间限制:1.0s   内存限制:999.4MB
问题描述
  给你一个非零整数,让你求这个数的n次方,每次相乘的结果可以在后面使用,求至少需要多少次乘。如24:2*2=22(第一次乘),22*22=24(第二次乘),所以最少共2次;
输入格式
  第一行m表示有m(1<=m<=100)组测试数据;
  每一组测试数据有一整数n(0<n<=100000000);
输出格式
  输出每组测试数据所需次数s;
样例输入
3
2
3
4
样例输出
1
2
2
import java.util.*;
public class chengfacishu {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		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 js=0;
			temp = n[i];
			while (temp/2!=0){
				if (temp%2==0) {
					js++;
				}else {
					js+=2;
				}
				temp/=2;
			}
			System.out.println(js);
		}
	}

}


0

评论 (0)

取消