-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (60 loc) · 1.2 KB
/
Copy pathmain.cpp
File metadata and controls
61 lines (60 loc) · 1.2 KB
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
54
55
56
57
58
59
60
61
//
// main.cpp
// leetcode050
//
// Created by 萧天牧 on 17/6/12.
// Copyright © 2017年 萧天牧. All rights reserved.
//
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
//double myPow(double x, int n) {
// if(x == 1 )
// return 1;
// if(x == -1)
// return n % 2 == 0 ? 1 : -1;
// if(n == INT_MIN)
// return 0;
// double res = 0;
// bool flag = (n >= 0) ? true:false;
// if(!flag)
// n = -n;
// if(x == 0)
// return 0;
// if(n == 0)
// return 1;
// res = myPow(x, n >> 1);
// if(n % 2 == 0){
// if(!flag)
// return 1 /(res * res);
// return res * res;
// }
// else{
// if(!flag)
// return 1 /(res * res*x);
// return res * res * x;
// }
//}
double myPow(double x, int n){
unsigned long long p;
double res = 1;
if(n < 0){
n = -n;
x = 1 / x;
}
p = n;
while(p){
if(p&1)
res *= x;
x *= x;
p = p>> 1;
}
return res;
}
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << myPow(34.00515, -3)<<endl;
cout << pow(34.00515,-3)<<endl;
return 0;
}