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

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

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

use std::io::{self, *};

fn main() {
    let s = stdin().lock().lines().next().unwrap().unwrap();
    let mut n = s.parse::<i32>().unwrap();
    let mut res = 0;
    while n != 0 {
        let r = n % 2;
        if r == 1 {
            res += 1;
        }
        n /= 2;
    }
    println!("{}", res);
}

全部评论

相关推荐

05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-23 14:10
柴子木:找个工作你还发上脾气了🤣
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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