題目連結

負數呢?爆int呢?

AC Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifdef LOCAL
#include <bits/stdc++.h>
using namespace std;

// tree node stuff here...
#endif

static int __initialSetup = []()
{
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    return 0;
}
();

class Solution
{
public:
    double myPow(double x, int n)
    {
        // x^n
        double base = 1;
        bool neg = n < 0;
        long long int nn = llabs((long long int)n); // wow abs(n) might overflow...
        while (nn > 0) {
            if (nn & 1)
                base = base * x;
            x *= x;
            nn >>= 1;
        }
        return neg ? 1 / base : base;
    }
};

#ifdef LOCAL
int main()
{
    cout << Solution().myPow(2.00000, -2147483648) << endl;
    return 0;
}
#endif