1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | class Solution { public : double Power( double base, int exponent) { int isOdd; if (0.0==base) return 0; if (exponent>0) {} else if (exponent<0) { exponent=-exponent; base=1.0/base; } else return 1; if (exponent==1) return base; if (exponent &0x01 )isOdd=1; //指数是奇数 int times; if (isOdd) times= log (exponent-1)/ log (2); else times= log (exponent)/ log (2); double result=base; while (times--) { result=result*result; } if (isOdd) result=result*base; return result; } }; |