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

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

https://www.nowcoder.com/practice/1b46eb4cf3fa49b9965ac3c2c1caf5ad?tpId=37&&tqId=21285&rp=1&ru=/ta/huawei/&qru=/ta/huawei/question-ranking

代码

#include<stdio.h>
#include<math.h>

int count(int in)
{
    int num=0;
    
    while(in>0)
    {
        num+=in&1;
        in = in>>1;
    }
    
    return num;
}

int main()
{
    int in;
    
    while(scanf("%d", &in) != EOF)
    {
            printf("%d\n", count(in));
    }
}

参考 https://www.nowcoder.com/profile/282085987/codeBookDetail?submissionId=114675403

全部评论

相关推荐

_mos_:忍耐王
点赞 评论 收藏
分享
04-25 19:29
已编辑
宁波大学 测试开发
被普调的六边形战士很高大:你我美牛孩
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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