public class { public double myPow(double x, int n) { if(n==0) return 1; if(x==1) return 1; if(x==-1) return (n%2)==0?1:-1; if(n == Integer.MIN_VALUE) return 0; if(n<0) { n = -n; x = 1/x; } double res = 1; while(n > 0) { if((n&1)==1) res *= x; x *= x; n >>= 1; } return res; } }
|
近期评论