示例1
输入:5
输出:2
实现代码:
import java.io.InputStream;
public class Main {
public static void main(String[] args) throws Exception {
InputStream in = System.in;
int len;
byte[] b = new byte[1024];
while ((len = in.read(b)) > 0) {
if (len == 1) {break;}
String str = new String(b,0,len-1);
int n = Integer.parseInt(str);
int count = numberOfOne(n);
System.out.println(count);
}
}
private static int numberOfOne(int n) {
int count = 0;
while (n != 0) {
count++;
n = n & (n-1);
}
return count;
}
}