首页 > 试题广场 >

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

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

数据范围:

输入描述:

输入一个整数



输出描述:

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

示例1

输入

5

输出

2

说明

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

输入

0

输出

0
使用bitset, 两行代码搞定
#include <iostream>
#include <bitset>
using namespace std;
int main(){
    int n;
    while(cin >> n){
        bitset<32> bs(n);
        cout << bs.count() << endl;
    }
    return 0;
}

发表于 2017-08-13 21:03:31 回复(2)
import java.util.Scanner;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scan = new Scanner(System.in);
		while(scan.hasNext()){
			int n = scan.nextInt();
			int count = 0;
			String str = Integer.toBinaryString(n);
			char[] cha = str.toCharArray();
			for(char c :cha){
				if(c=='1'){
					count++;
				}
			}
			System.out.println(count);
		}
	}

}


发表于 2016-06-13 14:43:42 回复(2)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        while (in.hasNext()) {
            int a=in.nextInt();
            int count=0;

            while (a != 0) {
                a&=a-1;
                count++;
            }

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


发表于 2017-06-06 14:36:10 回复(3)
#include<stdio.h>
int main()
{
    int m;
    while(scanf("%d",&m)!=EOF)
    {
        int cnt=0;
        while(m>0)
        {
            if(m%2==1)
            {
                cnt++;
            }
            m/=2;
        }
        printf("%d\n",cnt);
    }
    return 0;
}

发表于 2020-11-21 23:25:08 回复(1)
#include<iostream>

using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        int count = 0;
        while(n > 0)
        {
            if(n & 1) ++count;
            n >>= 1;
        }

        cout << count << endl;
    }
    
    return 0;
}

发表于 2017-07-06 11:07:07 回复(4)

while(line=readline()) {
    // 将输入先转为二进制,正则匹配1的个数
    numOf1 = parseInt(line).toString(2).match(/1/g).length;
    print(numOf1);
}
发表于 2020-02-17 17:57:28 回复(0)
#include <iostream>

using namespace std;

int  NumberOf1(int n) {
	int count = 0;
	while (n)
	{
		n = n&(n - 1);
		count++;
	}
	return count;
}


int main()
{
	int n;
	while(cin >> n)
        cout << NumberOf1(n)<<endl;
}

发表于 2019-09-15 23:16:23 回复(0)
//
//  main.cpp
//  题目:
//
//	算法:
//  Created by Rain on 16/03/2017.
//  Copyright © 2017 Rain. All rights reserved.
//

#include<iostream>
#include<string>
#include<algorithm>
#include<vector> 
using namespace std;

int main()
{
   	int a=0;
    while(cin>>a)
    {
        int b=0;
    	while(a)
    	{
        	a&=a-1;
        	b++;
    	}
    	cout<<b<<endl;
    }
    return 0;
}








发表于 2017-03-16 19:08:52 回复(0)
//一种简单的求法
import java.util.Scanner;
public class Main {
    public static void main(String args[]){
        Scanner input=new Scanner(System.in);
         while (input.hasNext()) {
             int n = input.nextInt();
             int count = 0;
             while (n > 0){
                 n = n & (n-1);
                 count ++;
             }
             System.out.println(count);
         }              
    }
}

发表于 2016-12-29 13:39:38 回复(1)
 GCC有一个叫做__builtin_popcount的内建函数,它可以精确的计算1的个数。尽管如此,不同于__builtin_ctz,它并没有被 翻译成一个硬件指令(至少在x86上不是)。相反的,它使用基于表的方法来进行位搜索。这无疑很高效并且非常方便。其他语言的使用者没有这个选项(尽管他们可以重新实现这个算法)。如果一个数只有很少的1的位,另外一个方法是重复的获取最低的1位,并且清除它。
#include<iostream>
using namespace std;
int main()
{
	int num;
	while(cin >> num)
        cout << __builtin_popcount(num) << endl;
	return 0;
}


发表于 2022-03-14 15:51:16 回复(0)
import java.util.*;
 
public class Main{
    public static void main(String []args){
        Scanner sc = new Scanner(System.in);//先扫描进来一段字符串    
        
        while(sc.hasNext()){//循环扫描
            int count = 0;//计数1的个数
            int n = sc.nextInt();//把字符串转换为整型
             String r = Integer.toBinaryString(n);//十进制整型再转换为二进制字符串
             for (int i = 0; i <= r.length() - 1; i++) {
                if (r.charAt(i) == '1') {//遍历这个二进制字符串,找到字符1就记数
                    count ++;
                }
            }
            System.out.println(count);//输出计数
        }
    }
}

发表于 2021-09-15 19:31:30 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a;
    while(cin>>a){
        int sum=0;
        while(a){
            sum+=a&1;
            a>>=1;
        }
        cout<<sum<<endl;
    }
    return 0;
}

发表于 2021-08-29 00:34:48 回复(0)
#include<stdio.h>

int main()
{
    int num,cnt;
    while(scanf("%d",&num)!=EOF)
    {
        cnt=0;
        while(num>0)
        {
            if(num%2==1)
                cnt++;
            num = num/2;
        }
        printf("%d\n",cnt);
    }
    
}

发表于 2021-08-21 18:50:22 回复(0)
对Java不太友好
import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()) {

            int count = 0, num = scanner.nextInt();
            
            while (num != 0) {
                count++;
                num &= num - 1;
            }

            System.out.println(count);
            
        }

    }
    
}


发表于 2021-07-21 16:19:32 回复(0)
位运算
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n) {
        int ans=0;
        while(n!=0) {
            ++ans;
            n&=(n-1);
        }
        cout<<ans<<endl;
    }
    return 0;
}

发表于 2021-06-27 16:02:32 回复(0)
python一行代码:
while True:
    try:
        print(format(int(input().strip()), 'b').count('1'))
    except:
        break

编辑于 2020-12-03 17:18:19 回复(0)
import java.util.Scanner;

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

差不多一行代码解决,利用Integer类bitCount函数,找出一个整型数2进制位有多少个1
发表于 2020-11-23 23:14:24 回复(1)
#include <iostream>

using namespace std;
//取余数即可
int main()
{
    int n;
    while(cin>>n)
    {
        int res = 0;
        while(n)
        {
            if(n%2 == 1)    res++;
            n = n/2;
        }
        cout<<res<<endl;
    }
}

发表于 2020-07-13 09:20:23 回复(0)
//使用位运算快速的将十进制转为2进制

#include<bits/stdc++.h>
using namespace std;

int main(){
#ifdef ONLINE_JUDGE
#else
	 freopen("E:/input.txt", "r", stdin);
#endif

    int n;
    while(cin >> n)
    {
        int ans = 0;
        for(int i = 31; i >= 0; --i)
        {
            ans += (n >> i & 1);
        }
        cout << ans << endl;
    }
    return 0;
}





编辑于 2020-03-14 21:53:01 回复(0)
//应用Integer.bitCount()
import java.util.*;

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

发表于 2020-03-07 15:11:03 回复(1)

问题信息

难度:
453条回答 25004浏览

热门推荐

通过挑战的用户

查看代码