指数递归实现,注意定义结果类型是double,不然负数的时候会变为0,导致奔溃
#include<iostream>
#include<string>
#include<cctype>
#include <vector>
#include<exception>
#include <initializer_list>
#include<stack>
using namespace std;
class Solution {
public:
double Power(double base, int exponent) {
int u = abs(exponent);
if (u == 1) {
return base;
}
double result= Power(base, u - 1)*base;
double result2 = 1 /result;
if (exponent < 0) {
return result2;
}if (exponent == 0) {
return 1;
}
else{ return result; }
}
};
int main()
{
Solution a;
//vector<int> in = { 4, 7, 2, 1, 5, 3, 8, 6 },b;
cout << a.Power(3,3);
}