topic
Given a float base of type double and an integer exponent of type int. Find the exponent power of base.
ideas
This topic is relatively simple, the main attention is to think comprehensively, the positive number, negative number of the index, and the situation of 0.
code
public class Solution {
public double Power(double base, int exponent) {
double result = 1;
if (exponent > 0) {
for (int i = 0; i < exponent; i++) {
result = base * result;
}
} else if ((exponent < 0)) {
double dao = 1 / base;
int ex = -exponent;
for (int i = 0; i < ex; i++) {
result = result * dao;
}
} else {
return 1;
}
return result;
}
}