这道题水过,用位运算!前几天做一道题感觉位运算很快,于是收集整理了下位运算的一些常见的应用
呵呵!这道题正好用上,水过,一次AC!
package poj2453; import java.util.Scanner; public class Main { /** * 获得一个Integer对应的二进制中1的个数 * @param i * @return sum */ public static int getSum(int i) { int sum = 0; while (i > 0) { i = i & (i - 1); ++sum; } return sum; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int i = sc.nextInt(); if (i == 0) break; int sum1 = getSum(i); for (int j = i + 1;; ++j) { int sum2 = getSum(j); if (sum2 == sum1) { System.out.println(j); break; } } } } }