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

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

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

#include <iostream>
using namespace std;
void solve(int x){
    int res = 0;
    while(x){
        if(x&1){
            res++;
        }
        x>>=1;
    }
    cout<<res<<'\n';
}
int main() {
    int a, b;cin>>a>>b;
    solve(a);solve(b);
    return 0;
}
// 64 位输出请用 printf("%lld")

跟快速幂很像,每次左移即可,统计1的个数。

活动地址https://www.nowcoder.com/discuss/726480854079250432

#牛客春招刷题训练营#
全部评论

相关推荐

DBsan:我也遇到过好的HR,全程友好交流。这年头基本的礼貌和尊重为什么好多HR都做不到
找工作时遇到的神仙HR
点赞 评论 收藏
分享
AC鸽想进大厂:你是我见过最美的牛客女孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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