码迷,mamicode.com
首页 > 其他好文 > 详细

华为OJ:查找输入整数二进制中1的个数

时间:2014-07-09 10:49:38      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:style   java   for   new   amp   size   

不得不说这道题就是一道十分蛋疼的题,我本来想着对输入的整数K对1按位与运算,然后再将K向右移一位,循环这样做,知道K为0。

万万没想到,程序一直超时,莫名其妙,看讨论上说是因为这编译器高位补零,还能不能再坑一点。

import java.util.Scanner;


public class binarySystemOneNumber {
	
	public static int findNumberOf1(int k){
		int count=0;
		int num=1;
		for(int i=1;i<=Integer.SIZE;i++){
			if((k&num)!=0)count++;
			num=2*num;
		}
		return count;
	}

	
	public static void main(String args[]){
		Scanner input=new Scanner(System.in);
		int k=input.nextInt();
		System.out.println( findNumberOf1(k));
	}
}

 

华为OJ:查找输入整数二进制中1的个数,布布扣,bubuko.com

华为OJ:查找输入整数二进制中1的个数

标签:style   java   for   new   amp   size   

原文地址:http://blog.csdn.net/whu_sky/article/details/37584043

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!