剑指Offer(牛客网)-数值的整数次方

题目来源:

https://www.nowcoder.com/practice/1a834e5e3e1a4b7ba251417554e07c00?tpId=13&tqId=11165&tPage=1&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking

题目描述:

给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

代码如下:

public class Solution {
	public double Power(double base, int exponent) {
		double result = 1;
		if (exponent == 0)
			return 1;
		int absExponent = exponent>0?exponent:-exponent;
		for (int i = 0; i < absExponent; i++)
			result *= base;
		if (exponent < 0)
			result = 1 / result;
		return result;

	}
}

 

你可能感兴趣的:(剑指Offer(牛客网),数值的整数次方,剑指Offer-数值的整数次方,剑指Offer(牛客网),牛客网编程题,剑指Offer)