power函数的非递归经典实现(时间复杂度仅仅为logn)

#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)

相关文章:

你感兴趣的文章:

标签云: