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

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

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

模2法运算,思路比较简单^ ^
#include<stdio.h>

int main(){
    int input;
    int count=0;
    scanf("%d",&input);
    while(input!=1&&input!=0){
        if(input%2==1)
            count++;
        input/=2;
    }
    if(input==1)
        count++;
    printf("%d",count);
    
    return 0;
}

全部评论

相关推荐

02-16 10:35
已编辑
西安科技大学 后端
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务