首页 > 试题广场 >

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

[编程题]求int型正整数在内存中存储时1的个数
  • 热度指数:381391 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入一个 int 型的正整数,计算出该 int 型数据在内存中存储时 1 的个数。

数据范围:保证在 32 位整型数字范围内

输入描述:

 输入一个整数(int类型)



输出描述:

 这个数转换成2进制后,输出1的个数

示例1

输入

5

输出

2
示例2

输入

0

输出

0
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Long a = in.nextLong();

        String str = Long.toBinaryString(a);

        char c = '1';
        int n = 0;

        for(int i=0;i<str.length();i++){
            if(str.charAt(i) == c){
                n++;
            }
        }
        System.out.println(n);
    }
}
编辑于 2024-04-18 17:09:19 回复(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();
        int count = 0;
        while (num > 0){
            if(num % 2 == 1){
                count++;
            }
            num = num / 2;
        }
        System.out.println(count);
    }
}
编辑于 2024-04-15 20:28:28 回复(0)
有好心人能告诉我,为什么按注释里的内容就得不出来正确结果 1+0=0;这个加号变成异或操作了

 public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        int count = num & 1;
        for (int i = 0; i < 7; i++) {
            int temp=(num >>>=1) & 1;
           // count = count + (num >>>=1) & 1 ;
            count = count + temp ;

        }
        System.out.println(count);

    }




编辑于 2024-04-13 14:03:58 回复(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 N = in.nextInt();
        String reString = Integer.toString(N, 2);
        int res = 0;
        for (char c : reString.toCharArray()) {
            if (c == '1') {
                res++;
            }
        }

        System.out.println(res);
    }
}
编辑于 2024-03-25 15:48:24 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        int n = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
        int count = 0;
        for (int x = 0; x < 32; x++) {
            count += (n & (1 << x)) == (1 << x) ? 1 : 0;
        }
        System.out.println(count);
    }
}
编辑于 2023-12-07 22:17:02 回复(0)
System.out.println(Integer.toBinaryString(new Scanner(System.in).nextInt()).chars().filter(ch -> ch == '1').count()); 
就是玩 
发表于 2023-11-08 17:58:36 回复(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) {
            if (num % 2 == 1) {
                count++;
            }
            num /= 2;
        }
        System.out.print(count);
    }
}

发表于 2023-10-25 14:09:25 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a1 = Integer.toBinaryString(in.nextInt());
        int a2 = 0;
        for (int i = 0; i < a1.length(); i++) {
            if (a1.charAt(i) == 49) {
                a2++;
            }
        }
        System.out.print(a2);
    }
}
直接先进行进制转化,然后统计1的个数
发表于 2023-10-23 01:59:53 回复(0)
import java.util.Scanner;

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

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        int count=0;
        // 注意 hasNext 和 hasNextLine 的区别
        while (!(n/2==0) || n%2==1) { // 注意 while 处理多个 case
            if(n%2==1){
                count++;
            }
            n/=2;
           
        }
        System.out.println(count);
    }
}
发表于 2023-08-12 23:01:28 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int number = Integer.parseInt(in.nextLine());
        int sum = 0;
        while (number != 0) {
            number <<= 1;
            sum += number < 0 ? 1 : 0;
        }
        System.out.println(sum);
    }
}

发表于 2023-08-09 00:34:54 回复(2)
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();
            int count=0;
            while(a!=0){
                a=a&(a-1);
                count++;
            }
            System.out.println(count);
        }
    }
}

发表于 2023-08-05 23:32:31 回复(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 inp=in.nextInt();
        int i=0;
        while(inp/2!=0)
        {
            if(inp%2==1)
            {
                i++;
            }
            inp/=2;

        }
        if(inp==1)
        i++;
        System.out.println(i);
    }
}
发表于 2023-07-10 14:21:08 回复(0)
方法1:
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int num = in.nextInt();
            System.out.println(Integer.bitCount(num));
        }
    }
}
方法二:
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int num = in.nextInt();
            String s = Integer.toBinaryString(num);
            String s1 = s.replaceAll("0", "");
            System.out.println(s1.length());
        }
    }
}
方法三:
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int num = in.nextInt();
            int count = 0;
            while(num>0){
                if(num%2==1){
                    count++;
                }
                num = num>>>1;
            }
            System.out.println(count);
        }
    }
}




发表于 2023-06-05 15:18:57 回复(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 的区别
        String str=Integer.toBinaryString(in.nextInt());
        int sum=0;
        for(int i=0;i<str.length();i++){
            if(str.charAt(i)=='1'){
                sum++;
            }
        }
        System.out.print(sum);
    }
}

发表于 2023-05-24 20:57:41 回复(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){
            double t = 1;
            for(int i=0;num>=Math.pow(2,i);i++){
                t = Math.pow(2,i);
            }
            count++;
            num=num%(int)t;
        }
        System.out.println(count);
    }

   
}

发表于 2023-05-22 23:51:34 回复(0)
    Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        int count = 0;
        String str = Integer.toBinaryString(a);
        for(int i= 0;i < str.length();i++){
            if(str.charAt(i)== '1'){
                count++;
            }
        }
        System.out.print(count);

发表于 2023-05-22 16:29:47 回复(0)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt(br.readLine());
        int count = 0;
        do {
            if ((a & 1) == 1 )  {
                count++;
            }
            a = a >> 1;
        } while (a > 0);
        System.out.print(count);
    }
}
发表于 2023-05-18 22:02:48 回复(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
            Integer a = in.nextInt();
            //Integer包装类中bitcount方法返回1的个数
            System.out.println(Integer.bitCount(a));
        }
    }
}
发表于 2023-04-09 21:42:25 回复(0)

问题信息

难度:
203条回答 98713浏览

热门推荐

通过挑战的用户

查看代码