小编use*_*554的帖子

我正在寻找一种计算数字功效的算法.(x ^ y),x和y是整数.它必须是复杂度O(log [n]))

目前,我的最大努力导致了复杂度O(log [n] ^ 2):

int power(x,n)
{
  int mult=1, temp=x, i=1, j=1;
  while (n>1)
  {
    mult=mult*x;
    x=temp;
    for (i=1;i<=log[n];i++)
    {
      x=x*x;
      j=j*2;
    }
    n=n-j;
    i=1;
    j=1;
  }
  if (n==1)
    return (mult*temp);
  return (mult);
}
Run Code Online (Sandbox Code Playgroud)

PS谢谢你funkymushroom帮助我的英语不好:)

algorithm math complexity-theory

2
推荐指数
1
解决办法
234
查看次数

标签 统计

algorithm ×1

complexity-theory ×1

math ×1