首页 > 代码库 > 统计2的N次方的数的个数

统计2的N次方的数的个数

package huawei;

import java.util.Scanner;

public class TestHuaWei {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int nCount = 0;
try {
if (in.hasNextLine()) {
String line = in.nextLine();

if (line != null && line.trim().length() > 128) {
System.out.println("输出错误");
}

String iArray [] = line.split(",");

for (int i = 0, size = iArray.length; i < size; i++) {

int num = Integer.valueOf(iArray[i]).intValue();
if (num == 1) {
nCount ++;
} else {
if ((num & (num - 1)) == 0) {
nCount ++;
}
}

}
}
}catch(Exception e) {
System.out.println("输入错误");
} finally {
if (in != null) {
in.close();
}
}

System.out.println(nCount);




}

}

统计2的N次方的数的个数