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

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

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

使用辗转相除法,每次除2,余数为1,则记录+1;直至除为0

利用while循环读入实现多组输入输出

#include <iostream>
using namespace std;

int main() {
    int a;
    while (cin >> a) {
        // cin >> a;
        int count = 0;
        while (a) {
            int div = a % 2;
            if (div == 1)
                count ++;
            a /= 2;
        }
        cout << count << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

神哥了不得:放平心态,再找找看吧,主要现在计算机也变卷了,然后就比较看学历了,之前高中毕业你技术强,都能找到工作的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务