首页 > 试题广场 >

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

[编程题]查找输入整数二进制中1的个数
  • 热度指数:151662 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入一个正整数,计算它在二进制下的1的个数。
注意多组输入输出!!!!!!

数据范围:

输入描述:

输入一个整数



输出描述:

计算整数二进制中1的个数

示例1

输入

5

输出

2

说明

5的二进制表示是101,有2个1   
示例2

输入

0

输出

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)
我想问下大家,Java牛客模板都是 IO的。可以改成Scanner吗
发表于 2022-08-19 18:22:13 回复(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)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        String toBinaryString = Integer.toBinaryString(num);
        String replaceAll = toBinaryString.replaceAll("0", "");
        System.out.println(replaceAll.length());
    }
}
发表于 2022-06-14 16:49:13 回复(1)
按位与并右移
import java.util.*;

public class Main {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       while(sc.hasNext()) {
           int number = sc.nextInt();
           int count = 0;
           while(number != 0) {
               if((number & 1) > 0) {
                   count++;
               }
               number >>= 1;
           }

           System.out.println(count);
       }
   }   
}

发表于 2022-06-10 03:40:30 回复(0)
位运算与& 位移

import java.util.*;

public class Main {
    public static void main(String[] args)  {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int count = sc.nextInt();
            int sum = 0;
            while (count > 0) {
                if ((count & 1) == 1) {
                    sum++;
                }
                count = count >>> 1;
            }
            System.out.println(sum);
        }
    }


}

发表于 2022-05-10 00:55:31 回复(0)
注意有多组输出可还行,删除全部的1,计算长度之差
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        while(sc.hasNext()){
            Integer num = new Integer(sc.nextInt());
            String res = Integer.toBinaryString(num);
            String res_ = res.replace("1", "");
            System.out.println(res.length() - res_.length());
        }
    }
}

发表于 2022-04-21 20:00:37 回复(0)

问题信息

难度:
61条回答 25086浏览

热门推荐

通过挑战的用户

查看代码