Problem K: 二进制表示中1的个数

来源:互联网 发布:百度一下淘宝商城 编辑:程序博客网 时间:2024/06/08 03:21

Description

给你一个数n,求n用二进制表示时,其中1的个数。

Input

有多组测试用例,每组一个n(0<=n<264)。

Output

n的二进制表示中1的个数。

Sample Input

0
2
7

Sample Output

0
1
3

HINT

使用长整形

#include <iostream>using namespace std;int main(){    unsigned long long int n;    while(cin>>n){        int i,res=0;        while(n){        i=n%2;        if(n==0&&i==1)        res++;        n=n/2;        if(i&1==1)        res++;        }    cout<<res<<endl;    }    return 0;    }
0 0
原创粉丝点击