forked from int32bit/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpow.c
53 lines (52 loc) · 848 Bytes
/
pow.c
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
50
51
52
53
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
const double ERROR = 1e-8;
int doubleEQ(double x, double y)
{
return fabs(x - y) < ERROR;
}
double myPow(double x, int n)
{
if (n == 0) {
return 1;
}
if (n == 1)
return x;
if (n == -1) {
if (fabs(x - 0) < ERROR) { // x位0 1/0 没有意义
return INT_MAX;
}
return 1.0 / x;
}
if (doubleEQ(x, 0)){
return 0;
}
if (doubleEQ(x, 1)) {
return 1;
}
if (doubleEQ(x, -1)) {
if ((n & 0x1))
return -1;
else return 1;
}
if ((n & 0x1)) {
int mid = (n - 1) / 2;
double half = myPow(x, mid);
return half * half * x;
} else {
double half = myPow(x, n / 2);
return half * half;
}
}
int main(int argc, char **argv)
{
double x;
int n;
while (scanf("%lf%d", &x, &n)) {
printf("%lf\n", myPow(x, n));
}
return 0;
}