题解 | #查找输入整数二进制中1的个数#

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

https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad

/*
 * @Description: If you get question about the code, contact me at **********.
 * @Author: pengjunxing
 * @Date: 2023-02-10 19:59:42
 * @LastEditors: pengjunxing
 * @LastEditTime: 2023-02-10 20:02:36
 */
#include<iostream>
using namespace std;
int count(int n)
{
    int time=0;
    while(n)
    {
        if(n%2)
        {
            time++;
        }
        n/=2;
    }
    return time;
}
int main()
{
    int num;
    while(cin>>num)
    {
        cout<<count(num)<<endl;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务