【剑指offer-JZ16】数值的整数次方

59 阅读1分钟
public class Solution {
    public static double Power(double base, int exponent) {
        if (exponent == 0)
            return 1;
        else if (exponent % 2 == 1 )   //  对于n为正数的情况
            return Power(base, exponent - 1) * base;
        else if (exponent % 2 == -1 )   //  对于n为负数的情况
            return Power(base, exponent + 1) / base;
        else
        {
            double temp = Power(base, exponent / 2);    //  对于n为负数的情况
            return temp * temp;
        }
    }

    public static void main(String[] args) {
        System.out.println(-1%2);

        System.out.println(Power(2.1000,-2));
    }
}



题源:www.nowcoder.com/practice/1a…