标签:
class Solution { public: double pow(double x, int n) { if(n==0) return 1; bool nNeg = false; long long int nn = n; if(n<0){ nn = - nn ; nNeg =true; } bool xNeg = false; if(x<0){ x = -x; if(n%2==1) xNeg = true; } double ret = 1; while(nn){ if(nn&1) ret *= x; x *=x; nn>>=1; } if(nNeg==true) ret = 1/ret; if(xNeg==true) return -ret; return ret; } };
标签:
原文地址:http://www.cnblogs.com/Azhu/p/4395037.html