题目描述
用递归算法求xn。
输入格式
一行,两个整数x和n。
输出格式
一行,一个整数xn
输入样例
5 6
输出样例
15625
#include<iostream>
using namespace std;
int x, n;
long power(int x, int exp)
{
if (exp == 1)
return x;
if (exp % 2 == 0)
{
long temp= power(x, exp / 2);
return temp * temp;
}
else
{
long temp = power(x, (exp - 1) / 2);
return x * temp * temp;
}
}
int main()
{
cin >> x >> n;
if (n == 0)
{
cout << 1;
return 1;
}
cout << power(x, n);
}