static boolean kkk(long x) {
if(x==1)
return true;
else {
// 初始化xx为1,用于计算2的幂
long xx = 1;
// 循环60次,检查2的幂是否等于x
for (int i = 1; i <= 60; i++) {
xx *= 2; // 每次将xx乘以2
if (xx == x) { // 如果xx等于x,说明x是2的幂,返回true
return true;
}
}
// 如果循环结束后没有找到匹配的2的幂,返回false
return false;
}
}
static long a[] = new long[210000];
static int count;
public static void main(String[]args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
for (int i = 1; i <= n; i++) {
a[i] = scan.nextLong();
}
// 遍历数组a中的每个数字,判断其是否是2的幂
for (int i = 1; i <= n; i++) {
if (kkk(a[i])) { // 如果数字是2的幂,计数器jjj加1
count++;
}
}
// 输出满足条件的数字个数
System.out.println(count);
}
数学还真是神奇,首先奇数肯定能被连续的数表示,比如 5 = 2 + 3 7 = 3 + 4 9 = 4 + 5
.....
实在太难想了,数学太奇妙了