LeetCode-Pow(x, n)

来源:互联网 发布:win10新系统优化 编辑:程序博客网 时间:2024/06/08 07:36
Implement pow(xn).
double searcht(double x,int m);double  output = 1;class Solution {public:    double pow(double x, int n);};//不知道采用迭代算法怎么样double Solution::pow(double x, int n){   int signal = 0;   if(n == 0) return 1;   if(n == -2147483648)   {       n = 2147483647;       searcht(x,n);       return 1/(output*x);   }   if(n < 0)   {    n = -n;    signal = 1;   }    searcht(x,n);    if(signal == 1)        return 1/output;    else        return output;};double searcht(double x,int m){    if(m == 1)    {output = x; return 1;}    if(m%2 == 0)    {   searcht(x,m/2);        output = output *output;    }    else    {   searcht(x,m/2);        output = output*output*x;    }};

0 0
原创粉丝点击