“`
double myPow2(double x, int n) {
double tmp;
if(n==1)
return x;
tmp=myPow2(x,n/2);
if(n%2==1) //奇数
return x*tmp*tmp;
else
return tmp*tmp;
}
double myPow(double x, int n) {
if(x==1.0) return x;
if(x==-1.0) return n%2==1?x:-x;
if(n==0) return 1;
if(n>0)
return myPow2(x,n);
if(n<0)
return 1/myPow2(x,-n);
}“`