高效率的取幂运算

计算X^N的常见算法是使用N-1次乘法自乘,时间复杂度为O(n)。使用下面的递归算法更好,,时间复杂度为O(logn):

template<class T>T Pow(T x, unsigned int N){if (0==N){return 1;}else if (1==N){return x;}else if (0==N%2){return Pow(x*x, N/2);}else{return Pow(x*x, N/2)*x;}}

别为荒漠的艰难而哭泣,只为奔流入海功成名就那一天,

高效率的取幂运算

相关文章:

你感兴趣的文章:

标签云: