题解 | #求int型正整数在内存中存储时1的个数#
求int型正整数在内存中存储时1的个数
https://www.nowcoder.com/practice/440f16e490a0404786865e99c6ad91c9
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); // 注意 hasNext 和 hasNextLine 的区别 int count = 0; String b = Integer.toString(a,2); //转换成2进制的字符串 String c[ ] = b.split(""); //转换成2进制的字符串数组 // while (in.hasNextInt()) { // 注意 while 处理多个 case for(int i=0;i<c.length;i++){ if(c[i].equals("1")){ //注意使用equals方法时一定要加双引号 ++count; } } System.out.println(count); }