题解 | #求int型正整数在内存中存储时1的个数#

求int型正整数在内存中存储时1的个数

https://www.nowcoder.com/practice/440f16e490a0404786865e99c6ad91c9

#include <iostream>
#include <cmath>
using namespace std;
//解题思路:int最大为2^32,从最大2^32遍历到最小2^0,依次整除,整除得到一就count++,并将数字减去2^i
int main() {
    int num;
    cin>>num;
    int count=0;
    for(int i=32; i>=0; --i){
        int temp = num/pow(2,i);
        if(temp == 1){
            count++;
            num -= pow(2,i);
        }
    }
    cout<<count;
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

练习生懒羊羊:开飞机把这个公司创飞吧
点赞 评论 收藏
分享
05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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