首页 > 试题广场 >

查找输入整数二进制中1的个数

[编程题]查找输入整数二进制中1的个数
  • 热度指数:165728 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的整数 nm,分别求解他们在二进制表示下的 1 的个数。

输入描述:
\hspace{15pt}第一行输入一个整数 n\left(0 \leqq n \lt 2^{31}\right) 代表需要求解的第一个数字。
\hspace{15pt}第二行输入一个整数 m\left(0 \leqq m \lt 2^{31}\right) 代表需要求解的第二个数字。


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

输入

5
0

输出

2
0

说明

\hspace{15pt}十进制 05 的二进制表示如下:
\hspace{23pt}\bullet\,十进制 (0)_{10} 等于二进制 (0)_{2}
\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{15pt}本题数据已进行规范,不再需要读入至文件结尾(2025/01/09)。
哈哈,不要造轮子
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            System.out.println(Integer.bitCount(in.nextInt()));
        }
    }
}


发表于 2025-04-07 17:08:18 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int decimal = in.nextInt();
            String binary = Integer.toBinaryString(decimal);
            System.out.println(binary.replaceAll("0", "").length());
        }
    }
}
发表于 2025-03-27 15:07:07 回复(0)
import java.util.ArrayList;
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()) {
            ArrayList<Integer> list2 = new ArrayList<>();
            ArrayList<Integer> list1 = new ArrayList<>();
            list1.add(in.nextInt());
            int count = 0;
            for(int i=0;i<list1.size();i++){
                while(list1.get(i)>0){
                    if(list1.get(i)%2==1){
                        count++;
                    }
                    list1.set(i,list1.get(i)/2);
                }
                list2.add(count);
                count = 0;
            }
            for(int i=0;i<list2.size();i++){
                System.out.println(list2.get(i));
            }
        }
    }
}
发表于 2024-11-29 19:20:16 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int m = sc.nextInt();
            String s = Long.toBinaryString(m);
            int count = s.length() - s.replace("1", "").length();
            System.out.println(count);
        }
    }
}
发表于 2024-08-04 09:16:48 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.nextLine();
            int i = Integer.parseInt(s);
            String binaryString = Integer.toBinaryString(i);
            int count = 0;
            for (int j = 0; j < binaryString.length(); j++) {
                if (binaryString.charAt(j) == '1') {
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}

发表于 2023-11-17 16:53:21 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            String b = Integer.toString(a,2);
            System.out.println(b.length()-b.replaceAll("1","").length());
        }
    }
}
发表于 2023-09-26 15:45:05 回复(0)
public void stringTo62( ){
    Scanner in = new Scanner(System.in);  while (true){ int a = in.nextInt();  System.out.println(Integer.bitCount(a));  }
}
发表于 2023-09-14 21:30:46 回复(0)
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine()) != null) {
            int n = Integer.parseInt(str.trim());
            //用于记录1的个数
            int count = 0;
            //int共32位,循环32次
            for (int i = 0; i < 32; i++) {
                //n & 1表示n二进制末位的值&1,结果为1则说明末位为1,结果为0说明末位为0
                if ((n & 1) == 1) count++;
                //n右移一位
                n = n >> 1;
            }
            System.out.println(count);
        }
    }
}


发表于 2023-06-26 17:13:02 回复(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 a = in.nextInt();
            String str = Integer.toBinaryString(a);
            int count = 0;
            for (int j = 0; j < str.length(); j++) {
                if (String.valueOf(str.charAt(j)).equals("1")) {
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}

发表于 2023-06-01 17:47:06 回复(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 i=in.nextInt();
            String str=Integer.toBinaryString(i);
            System.out.println(str.replace("0","").length());
        }
    }
}

发表于 2023-03-06 00:50:10 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            int n = in.nextInt();
            int count = 0;
            while(true){
                if(n % 2 == 1){
                    count++;
                }else if(n == 0){
                    System.out.println(count);
                    break;
                }
                n = n >> 1;
            }
        }
       
    }
}
发表于 2023-02-13 11:25:09 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) { 
            int n = in.nextInt();
            //思路一:先转二进制,再求1的数量
            /*
            String str = toBinary(n);
            System.out.println(str.replace("0","").length());
            */
            //思路二:在转的过程累加1的数量
            System.out.println(getOneCount(n));
        }
    }

    //思路二方法
    public static int getOneCount(int n){
        int count = 0;
        while(n >= 1){
            if(n % 2 == 1)
                count++;
            n = n/2;
        }
        return count;
    }

    //求取十进制转二进制字符串
    public static String toBinary(int n){
        //String str = Integer.toBinaryString(n);
        StringBuffer buffer = new StringBuffer();
        while(n > 1){
            buffer.append(n % 2);
            n = n / 2;
        }
        buffer.append(n);  //将最后的商拼接
        buffer.reverse();  //字符串反转,因为转二进制是反着连的
        return buffer.toString();
    }
}

发表于 2023-02-06 18:22:01 回复(0)

位运算解决方案

import java.util.Scanner;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int num = in.nextInt();
            int res = 0;
            for (int i = 0; i < 32; i++)
                // 1依次左移31位,每一次与num进行与运算
                if ((num & (1 << i)) != 0) res++;
            System.out.println(res);
        }
        in.close();
    }
}
发表于 2022-08-25 13:13:07 回复(0)
System.out.println(Integer.bitCount(sc.nextInt()));
发表于 2022-08-22 17:54:47 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while((str = br.readLine())!=null){
            int num = Integer.parseInt(str);
           // System.out.println(Integer.toBinaryString(num));
            int count = 0;
            for (int i = 0; i < 32; i++) {
                //如果末位为1则计数
                if ((num & 1) == 1) count++;
                //无符号右移
                num = num >>> 1;
            }
            System.out.println(count);
        }
    }
}

发表于 2022-08-19 15:53:18 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Scanner scanner = new Scanner(br);
        int result;
        while (scanner.hasNext()){
            result = countNumberOf1InBinary3(scanner.nextInt());
            System.out.println(result);
        }

    }
    // 解法一:通过与1进行&运算并右移,但遇到有符号数会陷入死循环     
    private static int countNumberOf1InBinary1(int num) {
        int count = 0;
        while (num != 0){
            if ((num & 1) == 1) count++;
            num >>= 1;
        }
        return count;
    }
   // 解法二:通过与 1 进行与运算,并将 1 左移,判断每一位是否为1。
      private static int countNumberOf1InBinary2(int n) {
       int count = 0, flag = 1;
        while (flag != 0) {
            if ((n & flag) != 0)
                count++;
            flag = flag << 1;
        }
        return count;
    }
    // 解法三: 一个数减去1后与自身进行与运算,得到的结果相当于把该数二进制最右边的1变为0      
    private static int countNumberOf1InBinary3(int n) {
        int count = 0;
        while (n != 0) {
            n = (n - 1) & n;
            count++;
        }
        return count;
    }  
 }        

发表于 2022-08-05 12:18:27 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextInt()){
            int n = sc.nextInt();
            int res = 0;
            for(int i=0;i<32;i++){
                if((n&1)==1){
                    res++;
                }
                n = n >> 1;
            }
            System.out.println(res);
        }
    }
}
发表于 2022-07-08 11:14:00 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int num = sc.nextInt();
            String s = Integer.toBinaryString(num);
            int count = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == '1') {
                    count++;
                }
            }
            System.out.println(count);
        }
    }
}
发表于 2022-06-26 15:03:22 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
       while (sc.hasNext()) {
            int bitCount = Integer.bitCount(sc.nextInt());
            System.out.println(bitCount);
        }
    }

}

发表于 2022-06-18 21:47:22 回复(0)

问题信息

难度:
65条回答 27358浏览

热门推荐

通过挑战的用户

查看代码
查找输入整数二进制中1的个数