#include<iostream>
using namespace std;
int function(int x, int n)
{
if(n == 0)
{
return 1;
}
int t = function(x, n/2);
if(n % 2 == 0)
{
return t * t; //even
}
else
{
return t * t * x; // odd
}
}
int main()
{
cout<<function(5, 3)<<endl;
cout<<function(5, 4)<<endl;
return 0;
}
out:
125
625
--------------------------------
Process exited after 0.4561 seconds with return value 0
请按任意键继续. . .