给定一个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 else if (exponent > 0) 7 { 8 9 double sum = 1; 10 for (int i = 0 ; i < exponent ;++i) 11 { 12 sum = sum*base; 13 } 14 return sum; 15 } 16 else 17 { 18 exponent = -1 * exponent; 19 double sum = 1; 20 for (int i = 0 ; i < exponent ;++i) 21 { 22 sum = sum/base; 23 } 24 return sum; 25 } 26 } 27 };