solution.md 3.1 KB
Newer Older
1
# Pow(x, n)
F
fix bug  
feilong 已提交
2

3
<p>实现 <a href="https://www.cplusplus.com/reference/valarray/pow/" target="_blank">pow(<em>x</em>, <em>n</em>)</a> ,即计算 x 的 n 次幂函数(即,x<sup><span style="font-size:10.8333px">n</span></sup>)。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>x = 2.00000, n = 10<strong><br />输出:</strong>1024.00000</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>x = 2.10000, n = 3<strong><br />输出:</strong>9.26100</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>x = 2.00000, n = -2<strong><br />输出:</strong>0.25000<strong><br />解释:</strong>2<sup>-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>-100.0 < x < 100.0</code></li>	<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code></li>	<li><code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code></li></ul>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
4
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
```cpp
int main()
{
    Solution sol;
    double x = 2.00000;
    int n = 10;
    double res;

    res = sol.myPow(x, n);

    cout << fixed << setprecision(5) << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
```cpp
class Solution
{
public:
    double myPow(double x, int n)
    {
        if (n == 0)
            return 1.0;
        unsigned long long u = llabs(n);
        double ans = 1.0;
        while (u)
        {
            if (u & 1)
                ans *= x;
            x *= x;
            u >>= 1;
        }
        return n > 0 ? 1.0 / ans : ans;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
56

57
### A
F
fix bug  
feilong 已提交
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
```cpp
class Solution
{
public:
    double myPow(double x, int n)
    {
        if (n == INT_MIN)
        {
            double t = dfs(x, -(n / 2));
            return 1 / t * 1 / t;
        }
        else
        {
            return n < 0 ? 1 / dfs(x, -n) : dfs(x, n);
        }
    }

private:
    double dfs(double x, int n)
    {
        if (n == 0)
        {
            return 1;
        }
        else if (n == 1)
        {
            return x;
        }
        else
        {
            double t = dfs(x, n / 2);
            return (n % 2) ? (x * t * t) : (t * t);
        }
    }
};
```

### B
F
fix bug  
feilong 已提交
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
```cpp
class Solution
{
public:
    double myPow(double x, int n)
    {
        if (n == 0)
            return 1;
        if (n % 2 == 1)
        {
            double temp = myPow(x, n / 2);
            return temp * temp * x;
        }
        else if (n % 2 == -1)
        {
            double temp = myPow(x, n / 2);
            return temp * temp / x;
        }
        else

        {
            double temp = myPow(x, n / 2);
            return temp * temp;
        }
    }
};

```

### C
F
fix bug  
feilong 已提交
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
```cpp
class Solution
{
public:
    double helper(double x, int n)
    {
        if (n == 0)
            return 1.0;
        double y = helper(x, n / 2);
        return n % 2 == 0 ? y * y : y * y * x;
    }

    double myPow(double x, int n)
    {
        long long N = static_cast<long long>(n);
        if (N == 0)
            return 1;
        return N > 0 ? helper(x, N) : 1. / helper(x, -N);
    }
};
```