Problem
Implement pow(x, n).
Code
double pow_r(double x, int n) { if(n == 0) return 1.0; double ret = pow_r(x*x,n / 2); if(n % 2 == 1) ret *= x; return ret; } double pow(double x, int n) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int m = abs(n); double ret = pow_r(x,m); return ((n < 0) ? 1.0/ret : ret); }
No comments:
Post a Comment