剑指offer-数值的整数次方

题目描述

给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
全面高效的递归算法:a^n=(a^n/2)*(a^n/2)
public class Solution {
    public double Power(double base, int exponent) {
       	 if(0==exponent)
	        	return 1;
	     if(exponent > 0){
			if (1 == exponent)
				return base;
			double result = Power(base, exponent >> 1);
			result *= result;
			if (1 == (exponent & 0x1))
				result *= base;
			return result;
	    }else {
             double nums = base;
            int flag = -exponent;
            for(int i = 1; i < flag; i++){
                nums = nums * base;
            }
            return 1/nums;
        
	    }
  }
}


你可能感兴趣的:(power,递归算法)