2.2-不要被阶乘吓倒

public class ch2_2 {
	public static int Nzero1(int x){
		int num=0;
		for(int i=1;i<=x;i++){
			int j=i;
			while(j%5 == 0){
				num++;
				j/=5;
			}
		}
		return num;
	}
	public static int Nzero2(int x){
		int num=0;
		while(x!=0){
			num += x/5;
			x /= 5;		  
		}
		return num;
	}
	public static int lowestOne1(int x){
		int num=0;
		while(x!=0){
			x>>=1;        //这两行先后顺序影响
		    num += x;
		}
		return num+1;
		
	}
	public static int lowestOne2(int x){
		int temp=x;
		int num=0;
		while(x!=0){
			x &= (x-1);
			num++;
		}
		return temp-num+1;
	}
	public static void main(String args[]){
		int x=10,y=3;
		System.out.println(x+"! 末尾0的个数(1):"+Nzero1(x));
		System.out.println(x+"! 末尾0的个数(2):"+Nzero1(x));
		System.out.println(y+"! 最低位1在(1):"+lowestOne1(y));
		System.out.println(y+"! 最低位1在(2):"+lowestOne2(y));
	}

}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页