題目描述
public class lertMove{
public static void main(String[] args) {
//使用n=10,二進制形式爲1010,則1的個數爲2;
int n = 8;
System.out.println(n + "的二進制中1的個數:" + Move(n));
}
public static int Move(int n) {
int count = 0;
while (n != 0) {
++count;
n = (n - 1) & n;
}
return count;
}
}
題目描述
//遞歸
public class Power{
public static void main(String[] args) {
Power x=new Power();
System.out.print(x.Powerx(2,-3));
}
public double Powerx(double base, int exponent) {
int n=Math.abs(exponent);
if(exponent<0) {
base=1/base;
}
if(n==1) {
return base;
}
return base*Powerx(base,n-1);
}
}