一
class Solution { public: double Power(double base, int exponent) { bool flag=false; double results=1; if(base==0 && exponent<0 ){ results=0; flag=true; return results; } int new_exponent=exponent; if(exponent<0) new_exponent=-exponent; double tmp=unsignedPower(base,new_exponent); if(exponent<0) results=1/tmp; else results=tmp; return results; } double unsignedPower(double base,int new_exponent){ if(new_exponent==0) return 1; double results=1; for(int i=1;i<=new_exponent;i++){ results*=base; } return results; } };
二
class Solution { public: double Power(double base, int exponent) { bool flag=false; double results=1; if(base==0 && exponent<0 ){ results=0; flag=true; return results; } int new_exponent=exponent; if(exponent<0) new_exponent=-exponent; double tmp=unsignedPower(base,new_exponent); if(exponent<0) results=1/tmp; else results=tmp; return results; } double unsignedPower(double base,int new_exponent){ if(new_exponent==0) return 1; if(new_exponent==1) return base; double results; int b=new_exponent/2; double tmp2=unsignedPower(base,b); results=tmp2*tmp2; if(new_exponent%2==1) results*=base; return results; } };
三
class Solution { public: double Power(double base, int exponent) { bool flag=false; double results=1; if(base==0 && exponent<0 ){ results=0; flag=true; return results; } int new_exponent=exponent; if(exponent<0) new_exponent=-exponent; double tmp=unsignedPower(base,new_exponent); if(exponent<0) results=1/tmp; else results=tmp; return results; } double unsignedPower(double base,int new_exponent){ if(new_exponent==0) return 1; if(new_exponent==1) return base; double results=unsignedPower(base,new_exponent>>1); results*=results; if(new_exponent & 0x1 ==1) results*=base; return results; } };