Implement pow(x, n), which calculates x raised to the power n (xn).
Note:
- -100.0 < x < 100.0
- n is a 32-bit signed integer, within the range [−231, 231 − 1]
public:
double myPow(double x, int n)
{
if(n==0)
return 1;
if(n<0)
{
if(n==INT_MIN)
{
n++;
if(x<0)
x=-x;
}
x=1/x;
n=-n;
}
if(n&1==1)
return x*myPow(x*x,n/2);
else
return myPow(x*x,n/2);
}
};