package leetcode; public class demo_50 { public double myPow(double x, int n) { if(n==0) {return 1.0;} if(n<0) { return 1.0/backtrack(x, -n); } return backtrack(x, n); } //二分法 public double backtrack(double x,int n) { double pow; if(n==0) { return 1.0; } pow=backtrack(x, n/2); if(n%2==0) { return pow*pow; } else { return x*pow*pow; } } public static void main(String[] args) { // TODO Auto-generated method stub demo_50 d50=new demo_50(); System.out.println(d50.myPow(2.00000,10)); } }
原文:https://www.cnblogs.com/Yshun/p/14823583.html