算法分析与设计课程——LeetCode刷题之 Pow(x, n)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38041038/article/details/78996939

题目:

Implement pow(xn).


Example 1:

Input: 2.00000, 10
Output: 1024.00000

Example 2:

Input: 2.10000, 3
Output: 9.26100
答案:

class Solution {
public:
     double pow(double x, int n) {
        if (n==0) return 1;
        double t = pow(x,n/2);
        if (n%2) {
            return n<0 ? 1/x*t*t : x*t*t;
        } else {
            return t*t;
        }
    }
};


猜你喜欢

转载自blog.csdn.net/m0_38041038/article/details/78996939