您的位置:首页 > 其它

[LeetCode]Pow(x, n)

2015-08-10 08:48 302 查看

题目

Number: 50

Difficulty: Medium

Tags: Math Binary Search

Implement pow(x, n).

题解

实现幂函数。

代码

[code]double myPow(double x, int n) {
    if( x == 1 || n == 0)
        return 1;
    double t = myPow(x, n / 2);
    if(n % 2)
        return n < 0 ? 1 / x * t * t : x * t * t;
    else
        return t * t;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: