https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00

逐项相乘

  1. public static double Power(double base, int exponent) {
  2. //保证不同时为0,先处理各自为0的情况
  3. if (exponent == 0) return 1.0;
  4. if (base == 0.0) return 0.0;
  5. double ans = 1.0;
  6. if (exponent < 0) {
  7. base = 1 / base;
  8. exponent = (-1) * exponent;
  9. }
  10. for (int i = 0; i < exponent; i++) {
  11. ans *= base;
  12. }
  13. return ans;
  14. }