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

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

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

// 输入数字%2,若不为0,则结果加1,输入减半
using System;

namespace HJ15{
    class Solution{
        public static void Main(){
            var num = Convert.ToInt32(Console.ReadLine()); 
            var res = 0;
            while(num != 0){
                if(num % 2 !=0){
                    res++;                  
                }
                num /= 2;
            }       
            Console.WriteLine(res);           
        }
    }
}

全部评论

相关推荐

双尔:你就写拥有ai开发经历,熟练运用提示词,优化ai,提高ai回答质量
点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

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