题解思路:一种是使用辗转相除法,将十进制转为二进制,在求解的过程中统计1出现的次数;一种是使用位运算,通过判断与1进行与运算的结果,来统计1出现次数
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Integer line = scanner.nextInt();
Integer count = count1NumberOfBinary1(line);
// Integer count = count1NumberOfBinary2(line);
System.out.println(count);
}
private static Integer count1NumberOfBinary1(Integer line) {
int mod,quotient,count=0;
while (true){
quotient = line/2;
mod = line%2;
if (mod == 1)
count++;
if (quotient == 0)
break;
else{
line = quotient;
}
}
return count;
}
}

京公网安备 11010502036488号