Implement pow(x, n)
這道題做出來很簡單缭付,但是加速稍微需要一些技巧漩氨。
temp = pow(x, n/2); ?//比如說 x^4 可以分成 x^2 ?* x^2
if (x%2 == 0) return temp * temp
else: return temp*temp*x ? 比如x^5 分成 x^2 * x^2 *x.
這里多乘的x是補上n/2時候四舍五入掉的一個x。
power如果是負(fù)數(shù)是一個非常Tricky的case:
-pow(x, -n)