题目链接:powx-n
/**
*
Implement pow(x, n).
*
*/
public class PowXN {
// 299 / 299 test cases passed.
// Status: Accepted
// Runtime: 343 ms
// Submitted: 0 minutes ago
static double pow(double x, int n) {
if(n < 0) {
return 1.0 / power(x, -n);
}else {
return power(x, n);
}
}
//绝壁得分治法,不然得超时或溢出
static double power(double x, int n) {
if(n == 0) return 1;
double half = power(x, n / 2);
if(n % 2 != 0) {
return x * half * half;
} else {
return half *half;
}
}
public static void main(String[] args) {
System.out.println(pow(0.00001, 2147483647));
}
}