-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpow50.cpp
49 lines (44 loc) · 1008 Bytes
/
pow50.cpp
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
43
44
45
46
47
48
49
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <string>
class Solution {
public:
double myPow(double x, int n)
{
std::function<double(double x, int n)> recurse;
recurse = [&](double x, int n) -> double {
if (x == 0) return 0;
if (n == 0) return 1;
double res = recurse(x * x, n / 2);
return (n % 2) ? res * x : res;
};
double res = recurse(x, std::abs(n));
return (n >= 0) ? res : 1 / res;
}
// double myPow(double x, int n)
// {
// bool neg = n < 0;
// if (neg) n = std::abs(n);
// double res = 1.;
// for (; n > 0; n--) {
// res *= x;
// }
// return neg ? 1 / res : res;
// }
};
int main()
{
double t11 = 2.5;
int t12 = 2;
double t21 = 69.69696969;
int t22 = 69;
double t31 = 2;
int t32 = -2;
Solution s;
std::cout << s.myPow(t11, t12) << std::endl;
std::cout << s.myPow(t21, t22) << std::endl;
std::cout << s.myPow(t31, t32) << std::endl;
return 0;
}