码迷,mamicode.com
首页 > 其他好文 > 详细

50. Pow(x, n) (INT; Divide-and-Conquer)

时间:2015-10-31 09:01:37      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

Implement pow(x, n).

class Solution {
public:
    double pow(double x, int n) {
        if(n == 0) return 1;
        return divideConquer(x,n);
    }
    double divideConquer(double x, int n)
    {
        double result;
        if(n==1) return x;
        if(n == -1) return 1/x;
        if(n%2 == 0)
        {
            result = divideConquer(x, n/2);
            result = result * result;
        }
        else
        {
            result = divideConquer(x, n/2);
            if(n>0)   result = result * result * x;
            else result = result * result / x;
        }
        return result;
    }
};

 

50. Pow(x, n) (INT; Divide-and-Conquer)

标签:

原文地址:http://www.cnblogs.com/qionglouyuyu/p/4924796.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!