首页 > 试题广场 >

多多的魔术盒子

[编程题]多多的魔术盒子
  • 热度指数:6319 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
多多鸡有N个魔术盒子(编号1~N),其中编号为i的盒子里有i个球。
多多鸡让皮皮虾每次选择一个数字X(1 <= X <= N),多多鸡就会把球数量大于等于X个的盒子里的球减少X个。
通过观察,皮皮虾已经掌握了其中的奥秘,并且发现只要通过一定的操作顺序,可以用最少的次数将所有盒子里的球变没。
那么请问聪明的你,是否已经知道了应该如何操作呢?



输入描述:
第一行,有1个整数T,表示测试用例的组数。
(1 <= T <= 100)
接下来T行,每行1个整数N,表示有N个魔术盒子。
(1 <= N <= 1,000,000,000)


输出描述:
共T行,每行1个整数,表示要将所有盒子的球变没,最少需要进行多少次操作。
示例1

输入

3
1
2
5

输出

1
2
3
import java.util.*;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for(int i = 0; i < n; i++) {
            int next = in.nextInt();
            double d = Math.log(next) / Math.log(2);
            int count = (int)Math.floor(d) + 1;
            System.out.println(count);
        }
    }
}

发表于 2023-07-07 14:47:28 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        while (num-- > 0) {
            int n = sc.nextInt();
            int count = 0;
            while (n > 0) {
                n = n >> 1;
                count++;
            }
            System.out.println(count);
        }
    }
}

发表于 2020-08-20 22:18:46 回复(0)
import java.util.Scanner;
public class Main
{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while(T -- > 0){
            int m = sc.nextInt();
            int res = 0;
            while(m > 0){
                m >>= 1;
                res ++;
            }
            System.out.println(res);
        }
    }
}
发表于 2020-08-10 16:57:20 回复(0)