This problem is deducted net force on 50 questions.
Achieve POW ( x , n ) , i.e., a function of n-th power of x.
Recursive and non-recursive thinking python implementation.
class Solution: # recurrent ideas DEF myPow_recursion (Self, X, n-): IF n-== 0: # recursive termination condition, n == 0 returns. 1 return . 1 IF n-<0: # n-less than 0, it returns to its reciprocal, -n and its recursive structure consistent return . 1 / self.myPow_recursion (X, - n-) IF n-. 1 &: # judgment is odd, the result is x * (x n-1 of power) return X * self.myPow_recursion ( x,. 1-n- ) the else : # judgment is even, the result is the square x * x n / 2-th power returnself.myPow_recursion (X * X, n / 2 ) # nonrecursive idea DEF myPow_loop (Self, X, n): IF n <0: # If n is less than 0, for the number n becomes positive, the reciprocal value n = - n X =. 1 / X POW =. 1 # result of the initial value. 1 the while n: # traversal cycle, has been divided by n is 2 IF n. 1 &: # odd time POW = X * # POW is X X X * = # X is multiplied by X n->>. 1 = # n-dividing continuity 2 return POW IF the __name__ == ' __main__ ' : Solution = Solution () # (2, -1) (2,0) (2,2 &) (2,5) X, = n-2, -2 Result = solution.myPow_recursion (X, n-) Print (Result) Result = solution.myPow_loop (X, n-) Print (Result)