题目
我的题解
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str = bf.readLine()) != null) {
int num = Integer.parseInt(str);
String binaryStr = Integer.toBinaryString(num);
int total = 0;
for (int i = 0; i < binaryStr.length(); i++) {
if (binaryStr.charAt(i) == '1') {
total++;
}
}
System.out.println(total);
}
}
}
总结:
1、 String binaryStr = Integer.toBinaryString(num);
关键点