1 public class Solution { 2 public double cal(double base, int exp) { 3 if (exp == 0) return 1; 4 if (exp == 1) return base; 5 double ans = cal(base, exp >> 1); 6 ans = ans * ans; 7 if (exp % 2 == 1) { 8 ans = ans * base; 9 } 10 11 return ans; 12 13 14 } 15 public double Power(double base, int exponent) { 16 if (base == 0) return 0; 17 int exp = exponent; 18 if (exponent < 0) exp = exp * -1; 19 double ans = cal(base, exp); 20 if (exponent < 0) { 21 ans = 1 / ans; 22 } 23 24 return ans; 25 26 } 27 }
原文:https://www.cnblogs.com/hyxsolitude/p/12305644.html