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

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

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

public class Main {
     public static void main(String[] args){
         Scanner sc = new Scanner(System.in);
         while (sc.hasNext()) {
            int num= sc.nextInt();
            //把数字转成二进制
            String s = Integer.toBinaryString(num);
            int cnt = 0;
            //字符串转数组
            char[] chars = s.toCharArray();
            //对chars进行循环
            for (char aChar : chars) {
                if (aChar=='1'){
                    cnt++;
                }
            }
            System.out.println(cnt);
        }
     }
}
全部评论

相关推荐

一只代码牛:应该不是你的问题,我感觉应该是最近不缺人
我的求职进度条
点赞 评论 收藏
分享
05-10 16:48
门头沟学院 Java
程序员小白条:主要原因,投递太晚了,快手应该早点溜了,你都从去年9月开始的,半年也差不多3月跑路了,这样的话,至少有5个以上的面试机会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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