# include <iostream>
using namespace std;
class Solution {
public:
double pow(double x, int n){
if(n<0) return 1.0/power(x,-n);
else return power(x,n);
}
private:
double power(double x, int n){
if (n==0) return 1;
double v = power(x, n/2);
if (n&1==0) return v*v;
else return v*v*x;
}
};
int main(){
cout<<Solution().pow(2,5)<<endl;
cout<<Solution().pow(3,4)<<endl;
return 0;
};
分治法 求幂
最新推荐文章于 2021-11-11 19:17:16 发布