#include<iostream>
using namespace std;
template <class T,class Integer>
T power(T x,Integer n)
{
if(n == 0) return 1;
if(n == 1) return x;
while((n&1) == 0)
{
x = x*x;
n >>= 1;
}
T result = x;
n >>= 1;
while(n != 0)
{
x = x*x;
if((n&1) != 0)
result *= x;
n >>= 1;
}
return result;
}
int main()
{
int x = 2;
int n = 30;
cout<<power<int,int>(x,n)<<endl;
return 0;
}
power函数的非递归经典实现(时间复杂度仅仅为logn)
最新推荐文章于 2025-03-18 17:12:52 发布