题目描述
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
1 class Solution { 2 public: 3 double Power(double base, int exponent) { 4 if (exponent == 0) 5 return 1; 6 if (exponent < 0){ 7 base = 1 / base; 8 exponent *= -1; 9 } 10 double result = Power(base, exponent / 2); 11 result *= result; 12 if (exponent % 2 != 0) 13 result = base * result; 14 return result; 15 } 16 };