首页 > 试题广场 >

求int型正整数在内存中存储时1的个数

[编程题]求int型正整数在内存中存储时1的个数
  • 热度指数:417074 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的 int 型的十进制整数 n,统计其在内存中存储时 1 的个数。换句话说,即统计其二进制表示中 1 的个数。

输入描述:
\hspace{15pt}在一行上输入一个整数 n \left( 0 \leqq n < 2^{31} \right),代表给定的数字。


输出描述:
\hspace{15pt}在一行上输出一个整数,代表 n 的二进制表示中 1 的个数。
示例1

输入

10

输出

2

说明

\hspace{15pt}十进制 110 的二进制表示如下:
\hspace{23pt}\bullet\,十进制 (1)_{10} 等于二进制 (1)_{2}
\hspace{23pt}\bullet\,十进制 (2)_{10} 等于二进制 (10)_{2}
\hspace{23pt}\bullet\,十进制 (3)_{10} 等于二进制 (11)_{2}
\hspace{23pt}\bullet\,十进制 (4)_{10} 等于二进制 (100)_{2}
\hspace{23pt}\bullet\,十进制 (5)_{10} 等于二进制 (101)_{2}
\hspace{23pt}\bullet\,十进制 (6)_{10} 等于二进制 (110)_{2}
\hspace{23pt}\bullet\,十进制 (7)_{10} 等于二进制 (111)_{2}
\hspace{23pt}\bullet\,十进制 (8)_{10} 等于二进制 (1000)_{2}
\hspace{23pt}\bullet\,十进制 (9)_{10} 等于二进制 (1001)_{2}
\hspace{23pt}\bullet\,十进制 (10)_{10} 等于二进制 (1010)_{2}
示例2

输入

0

输出

0
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int number = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        // 十进制转二进制
        while (number > 0) {
            sb.insert(0, number % 2);
            number /= 2;
        }
        // 覆盖0
        String ans = sb.toString().replaceAll("0", "");
        System.out.println(ans.length());
    }
}
发表于 2025-07-25 22:32:34 回复(0)
简单位运算问题,用Integer.toBiaryString(n)把十进制n转换为二进制字符串,遍历字符串统计字符‘1’的个数,最后输出控制台即可。
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            String s = Integer.toBinaryString(n);
            int res = 0;
            for (int i = 0; i < s.length(); i++){
                if (s.charAt(i) == '1'){
                    res++;
                }
            }
            System.out.println(res);
        }
    }
}

发表于 2025-07-20 00:39:59 回复(0)
import java.util.Scanner;
import java.math.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BigInteger a = in.nextBigInteger();
        String s1 = a.toString(2);
        s1 = s1.replace("0", "").trim();
         System.out.println(s1.length());
       
    }
}
BigInteger 处理转二进制
发表于 2025-07-11 22:44:08 回复(0)
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        String str = Integer.toBinaryString(num);
        int sum = 0;
        for(int i=0;i<str.length();i++){
            sum = sum + Integer.valueOf(str.charAt(i)+"");
        }
        System.out.println(sum);
    }

发表于 2025-07-06 21:21:41 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int num = in.nextInt();
        int count = 0;
        while(num != 0){
            count = count + (num & 1);
            num = num>>1;
        }
        System.out.print(count);
    }
}
发表于 2025-06-15 17:37:58 回复(0)

import java.util.Scanner;
import java.math.BigInteger;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        BigInteger integer = new BigInteger(String.valueOf(a));
        String binaryOutput = integer.toString(2);
        int count = 0;
        for(int i=0;i<binaryOutput.length();i++){
            if(binaryOutput.charAt(i)-'0'==1){
                count +=1;
            }
        }
        System.out.print(count);
    }
}

发表于 2025-05-21 23:13:47 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();

        int count = 0; //记录1出现的次数
        //10进制整数转换成2进制:用十进制数除以 2,得到商和余数。
        //                     将余数记录下来,这个余数就是二进制数的最低位。
        //                     用得到的商继续除以 2,重复上述步骤,直到商为 0。
        //                     从下往上读取所有的余数,得到的就是十进制数整数部分对应的二进制数
        while (n > 0) {
            if (n % 2 == 1) {
                count = count + 1;
            }
            n = n / 2;
        }

        System.out.println(count);
    }
}

发表于 2025-05-17 23:19:16 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            System.out.println(Integer.bitCount(in.nextInt()));
        }
    }
}

发表于 2025-04-24 21:17:41 回复(0)
1. 每次和1做&比较,1的二进制只有最低位是1,所以如果得到的结果是1,那么就证明对应位置是1,统计次数+1
2. 原数据使用>>操作向右移动移位,继续比较下一位
3. 只要数据大于0就循环
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int i = in.nextInt();
        int count = 0;
        while(i>0){
            if((i & 1) == 1){
                count++;
            }
            i = i >> 1;
        }
        System.out.print(count);
    }
}



发表于 2025-02-07 17:59:16 回复(0)
Scanner in = new Scanner(System.in);
String s = Integer.toBinaryString(in.nextInt());  // 转2进制
System.out.print(s.length() - s.replaceAll("1", "").length());  // 1的个数
in.close();

发表于 2025-01-26 21:59:26 回复(0)
import java.util.Scanner;

public class Main11 {

	public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
        	int number = in.nextInt();
            String str = Integer.toBinaryString(number);
            String times1 = str.replace("0", "");
            System.out.println(times1.length());
        }
        in.close();
    }
}

发表于 2024-09-24 10:03:25 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        int count = 0;
        while (num > 0) {  // 不断除二取余即可
            count += num % 2;
            num /= 2;
        }
        System.out.print(count);
    }
}
发表于 2024-09-17 10:47:20 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = Integer.toBinaryString(in.nextInt());
        String noOne = str.replace("1", "");
        System.out.print(str.length() - noOne.length());
    }
}

发表于 2024-09-13 20:02:01 回复(0)
方法一:采用Integer.bitCount()方法计算,经过Java底层优化,速度快。
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
           Scanner sc = new Scanner(System.in);
        // System.out.println("请输入您要计算的数:");
        int n = sc.nextInt();
        System.out.println(Integer.bitCount(n));
    }
}
方法二:转化成字符数组计算字符1出现的次数。
//        Scanner sc = new Scanner(System.in);
//        System.out.println("请输入要计算的数字:");
//        int n = sc.nextInt();
//        String binaryString = Integer.toBinaryString(n);
//        char[] charArray = binaryString.toCharArray();
//        int count = 0;
//        for (char c : charArray) {
//            if(c == '1'){
//                count++;
//            }
//        }
//        System.out.println(count);



发表于 2024-09-13 14:51:33 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int number = in.nextInt();
            int count = 0;
            while (number > 0) {
                if (number % 2 == 1) {
                    count++;
                }
                number /= 2;
            }
            System.out.println(count);
        }
    }
}
发表于 2024-09-06 16:32:23 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
       int x=in.nextInt();
       int num=0;
         String binaryString = Integer.toBinaryString(x);
         for(int i=0;i<binaryString.length();i++){
           if(binaryString.charAt(i)=='1')
           num=num+1;
         }
          System.out.println(num);
    }
   
}
发表于 2024-09-05 15:46:03 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String string = Integer.toBinaryString( in.nextInt());
        System.out.print(string.length() - string.replace("1","").length());
    }
}
发表于 2024-07-15 20:26:11 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int count = 0;
        while (num > 0) {
            if (num % 2 != 0) {
                count++;
            }
            num = num >> 1;
        }
        System.out.println(count);
    }
}

发表于 2024-07-03 21:48:52 回复(0)