首页 > 其他 > 详细

[Leetcode]50. Pow(x, n)

时间:2016-07-05 22:36:15      阅读:268      评论:0      收藏:0      [点我收藏+]

Implement pow(xn).

 

 

#define EPSINON 0.00001
#define Max 2147483647
#define Min -2147483648
#define DBL_MAX 1.7976931348623159e+308 

class Solution {
public:
    double myPow(double x, int n) {
        /*
        three special case
        */
        if(n==1)
            return x;
        if(n==0)
            return 1.0;
        if(abs(x)==1.00000){
            if(n%2==0)
                return 1.0;
            else 
                return x;
        }
            
        if(n>=Max){
            if(abs(x)<=EPSINON)
                return 0.0;
            else
                return DBL_MAX;
        }

        if(n<=Min){
            if(abs(x)<=EPSINON)
                return DBL_MAX;
            else
                return 0.0;
        }
                       
        int size=abs(n);
        double tmp=x;
        for(int i=2;i<=size;i++){
            tmp*=x;
        }
        if(n>=0)
            return tmp;
        else 
            return 1.0/tmp;
    }
};

 

[Leetcode]50. Pow(x, n)

原文:http://www.cnblogs.com/LUO77/p/5645127.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!