这题有点恶心的地方在于,n不是按顺序给出的,所以我们还是要去分析题目意思,来找出规律
经过分析, f函数其实就是拿来求n中1的个数的,那么我们可以直接使用Long.bitcount()函数来直接求
然后就是求出个数以后,我们需要知道第一次出现的n是多少
其实就是2的a次方-1
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
ArrayList<Long> list=new ArrayList();
while(t-->0) {
long n=sc.nextLong();
long a=f(n);
long b=(long)(Math.pow(2,a)-1);
System.out.println(a+" "+b);
}
}
public static long f(long a) {
return Long.bitCount(a);//统计一个数中1的个数
}
}



京公网安备 11010502036488号