static boolean kkk(long x) {if(x==1)return true;else {// 初始化xx為1,用于計算2的冪long xx = 1;// 循環60次,檢查2的冪是否等于xfor (int i = 1; i <= 60; i++) {xx *= 2; // 每次將xx乘以2if (xx == x) { // 如果xx等于x,說明x是2的冪,返回truereturn true;}}// 如果循環結束后沒有找到匹配的2的冪,返回falsereturn 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加1count++;}}// 輸出滿足條件的數字個數System.out.println(count); }
?數學還真是神奇,首先奇數肯定能被連續的數表示,比如 5? = 2 + 3? ? ?7 = 3 + 4? ?9 = 4 + 5
.....?
實在太難想了,數學太奇妙了?