题目要求:
Implement pow(x, n)
代码实现:
#include <iostream>
#include<vector>
using namespace std;
class Solution {
public:
double pow(double x, int n) {
double result;
if(n==0)
return 1;
else if(n>0)
{
result=pow(x,n/2);
if(n%2==0)
return result*result;
else
return result*result*x;
}
else
{
n*=(-1);
result=pow(x,n/2);
if(n%2==0)
return 1/result/result;
else
return 1/result/result/x;
}
}
};
void main()
{
Solution s;
cout<<s.pow(2,-4)<<endl;
getchar();
}