4
头图

Integer power

Title description

Given a floating-point base of type double and an integer exponent of type int. Find the exponent power of base.

  • Ensure that base and exponent are not equal to 0 at the same time.

title link : integer power

Code

/**
 * 标题:数值的整数次方
 * 题目描述
 * 给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。
 * 保证base和exponent不同时为0
 * 题目链接:
 * https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13&&tqId=11165&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
 */
public class Jz12 {

  public double Power(double base, int exponent) {
    double result = 0;
    if (exponent > 0) {
      result = base;
      while (exponent > 1) {
        result *= base;
        exponent--;
      }
    } else {
      result = 1;
      while (exponent <= -1) {
        result /= base;
        exponent++;
      }
    }
    return result;
  }

  public static void main(String[] args) {
    Jz12 jz12 = new Jz12();
    System.out.println(jz12.Power(2, 3));
    System.out.println(jz12.Power(2, -3));
  }

}
[Daily Message] Time will never go retrograde, take care of every morning that belongs to you.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!