这道题很简单,只需要对指数进行分情况讨论即可。
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution { public: double Power(double base, int exponent) { if(exponent==0) return 1; else if(exponent>0) return getResult(base,exponent); else return 1/getResult(base,-exponent); } double getResult(double base,int exp){ double result=1; for(int i=0;i<exp;i++) result*=base; return result; } };
|