8, the number of binary 1

Subject description:

An integer that indicates the number of the output of the binary number 1 in.

Wherein a negative number indicates a complement.

Problem-solving ideas: the operational

A non-zero integer, at least one of 1. n minus 1 in the rightmost binary 1 to 0, 0 to 1 behind

n and n-1 to 1 and start from the far right all becomes 0, if carried out several operations last 1 equal to 0, there are several.

After the Save 1101 such as 110 and 100 requirements, the same operation as the last number is the number of 0 2 = 1 again

 public static int numberOf2(int n) {
               int count = 0;
               while(n!= 0){
                   count++;
                   n = n & (n - 1);
                }
               return count;
    }

 

Subject description:

Given a double base type and floating-point type int integer exponent.

Seeking exponent of the power base.

  public double Power(double base, int exponent) {
                double sum=1;
        boolean flag=false;
        if(exponent<0){//判断是否大于0
         exponent=-exponent;
         flag=true;
        }
        while(exponent>=1){
            sum*=base;
            exponent--;
        }
        if(flag){
            return 1/sum;
        }
        return sum;  
  }

 

Guess you like

Origin www.cnblogs.com/kobe24vs23/p/11334938.html