第一种:时间复杂度O(logn)
#include<iostream>
using namespace std;
int POW(int a,int b)
{
if(b==0) return 1;
if(b&1) return a * POW(a, b - 1); //判断条件为是否是奇数(位运算)
else
{
int y = POW(a, b/2);
return y * y;
}
}
int main()
{
int x, n;
cin >> x >> n;
int result = POW(x, n);
cout << result;
return 0;
}
第一种:时间复杂度O(n)
#include<iostream>
using namespace std;
int POW(int a,int b)
{
if(b == 0) return 1;
return a * POW(a, b - 1);
}
int main()
{
int x, n;
cin >> x >> n;
int result = POW(x, n);
cout << result;
return 0;
}