首页 > 试题广场 >

整数的二进制数表达中有多少个1

[编程题]整数的二进制数表达中有多少个1
  • 热度指数:1215 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个32位整数n,返回该整数二进制形式1的个数。

输入描述:
输入一个整数,代表n,n为32为整数。


输出描述:
输出一个整数,代表n的二进制表达式中1的个数。
示例1

输入

1

输出

1
示例2

输入

-2

输出

31

备注:
时间复杂度,额外空间复杂度

使用C++库函数中的bitset

#include <iostream>
#include <bitset>
using namespace std;
int main(){
    int x;
    cin>>x;
    cout<<bitset<32>(x).count();
    return 0;
}
发表于 2019-09-30 15:17:47 回复(1)
num每次跟num-1按位与消去一个1,直到数字变为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 num = Integer.parseInt(br.readLine().trim());
        int count = 0;
        while(num != 0){
            num &= num - 1;
            count ++;
        }
        System.out.println(count);
    }
}

发表于 2021-06-12 23:19:57 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main(){
    int n, cnt=0;
    cin>>n;
    if(n<0){
        n += INT_MAX+1;
        cnt = 1;
    }
    while(n){
        cnt += n&1;
        n >>= 1;
    }
    cout<<cnt<<endl;
    return 0;
}

发表于 2020-05-24 01:11:13 回复(0)
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int n = scanner.nextInt();
        
        int res = 0;
        int i = 32;
        while(i-->0){
            res += (n & 1);
            n >>= 1;
        }
		System.out.print(res);
	}
}

发表于 2019-10-22 12:21:29 回复(0)
#include<iostream>
using namespace std;

int re=0;
int n;

int lowbit(int x)
{
    return x&(-x);
}

int main()
{
    cin>>n;
    while(n)
    {
        re++;
        n-=lowbit(n);
    }
    cout<<re<<endl;
    return 0;
}

发表于 2022-04-25 16:37:28 回复(0)
import java.util.*;

public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int res = count2(n);
        System.out.println(res);
        sc.close();
    }
    
    //最多遍历32次 不断看最右边的bit是否为1
    public static int count0 (int n) {
        int res = 0;
        while (n != 0) {
            res += n & 1;
            n >>>= 1;
        }
        return res;
    }
    
    //不断减去最右边的1
    public static int count1 (int n) {
        int res = 0;
        while (n != 0) {
            n -= n & (~n + 1);
            res++;
        }
        return res;
    }
    
    //不断抹除掉最右边的1
    public static int count2 (int n) {
        int res = 0;
        while (n != 0) {
            res++;
            n &= n-1;
        }
        return res;
    }
}

发表于 2021-08-09 00:07:47 回复(0)
留神题目要求:”返回…1的个数”,第一遍提交自己误以为求最高位的1位置。
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int result = 0;
        int decimal = 0;
        if (in.hasNextInt()) {
            decimal = in.nextInt();
            if(decimal >= 0) {
                result = new Main().getNumOf1(decimal);
            }else {
                result = 32-new Main().getNumOf1(-1-decimal);
            }
            System.out.println(decimal == 0? 0: result);
        }
    }

    public int getNumOf1(int decimal) {
        int res = 0;
        while(decimal/2 != 0) {
            res += (decimal%2);
            decimal >>>= 1;
        }
        return decimal ==1 ? ++res: res;
    }
}

发表于 2021-05-24 08:53:01 回复(0)
//题目是不是搞错了,实际是要32位的二进制吧。
#include<iostream>
#include<string>
#include<bitset>
using namespace std;
int main()
{
	unsigned int n;
	while (cin >> n)
	{
		bitset<32>b(n);
			cout << b.count() << endl;
	}
}

编辑于 2020-07-22 11:45:59 回复(0)
不用循环32次的代码
#include <iostream>
using namespace std;

int CountOne(int n){
    int time = 0;
    while(n != 0){
        n = n & (n-1);
        ++time;
    }
    return time;
}

int main(){
    int n;
    cin>>n;
    int ans = CountOne(n);
    cout<<ans<<endl;
    return 0;
}


发表于 2020-07-13 22:57:07 回复(0)

为什么总是报

a.cpp:22: Error: symbol 'LOOP_START' is already defined
a.cpp:26: Error: symbol 'NONZERO_TEST' is already defined

label改成什么都说重复定义!

#include <iostream>

int count(const int n){
    int res = 0;
    asm(
        "push %%rsi\n"
        "xor %%eax, %%eax\n"
        "jmp NONZERO_TEST\n"
        "LOOP_START:"
        "mov %%edi, %%esi\n"
        "and $1, %%esi\n"
        "add %%esi, %%eax\n"
        "shr %%edi\n"
        "NONZERO_TEST:"
        "test %%edi, %%edi\n"
        "jne LOOP_START\n"
        "pop %%rsi\n"
        :"=a"(res)
    );
    return res;
}

int main(){
    using namespace std;
    int n;
    cin >> n;
    cout << count(n) << endl;
    return 0;
}
发表于 2020-06-02 12:05:18 回复(0)

问题信息

上传者:小小
难度:
10条回答 4633浏览

热门推荐

通过挑战的用户

查看代码