30 Days of Code [Day 1]
QUESTION : POW(X,N)
CODE:
#define vi vector<int>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define fo(i,s,n) for(int i=s;i<n;++i)
#define of(i,s,n) for(int i=s-1;i>=n;--i)
#define printv(v) for(auto &x : v){cout<<x<<" ";}cout<<endl;
class Solution {
public:
double myPow(double x, int n) {
double t;
if(!n) return 1;
t = myPow(x, n/2);
if(n % 2 == 0){
return t*t;
}
else{
if(n > 0) return x*t*t;
else return (t*t)/x;
}
}
};