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

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

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

#include<stdio.h>

int main(){

int number;
while(scanf("%d", &number) !=EOF){
    int count = 0;
    for(int i=0; i<31; i++){
        if(number & 1 == 1)count++;
        number >>= 1;
    }
    printf("%d\n", count);
}

}

全部评论
int是32位的,题目要求是n位正数,所以最高位符号位一定为0,所以代码中i<31,不看最高位符号位。
2 回复 分享
发布于 2024-04-21 10:31 黑龙江
# include <stdio.h> int main() { int number; while (scanf("%d", &number) != EOF) { int count = 0; for (int i = 0; i < 31; i++) { if ((number & 1) == 1) count++; number >>= 1; } printf("%d\n", count); } }</stdio.h>
点赞 回复 分享
发布于 2024-07-09 01:59 重庆

相关推荐

身边有人上海、深圳&nbsp;6、7k&nbsp;都去了,真就带薪上班了。
程序员小白条:木的办法, 以后越来越差,还是家附近宅着吧,毕业的人越来越多,岗位都提供不出来,经济又过了人口红利期
点赞 评论 收藏
分享
测试糕手手:社会第一课,随便吹牛逼,直接说四个月,别老实。老实人只会被欺负
点赞 评论 收藏
分享
评论
8
1
分享

创作者周榜

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