Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 分而治之的权力;征服算法_Algorithm_Recursion_Data Structures_Divide And Conquer - Fatal编程技术网

Algorithm 分而治之的权力;征服算法

Algorithm 分而治之的权力;征服算法,algorithm,recursion,data-structures,divide-and-conquer,Algorithm,Recursion,Data Structures,Divide And Conquer,我想找出计算X^46时,用最优D&C方法计算功率会发生多少次乘法 我认为这是用分治法计算功耗的最佳代码 int power(int x, unsigned int y) { int temp; if( y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; }

我想找出计算X^46时,用最优D&C方法计算功率会发生多少次乘法

我认为这是用分治法计算功耗的最佳代码

int power(int x, unsigned int y)
  {
     int temp;
     if( y == 0)
       return 1;
     temp = power(x, y/2);
     if (y%2 == 0)
       return temp*temp;
     else
       return x*temp*temp;
  }
在D&C中用最优幂码计算X^46的一个注释中,我们需要8次乘法,但在我的代码中有10次。有人纠正我吗

编辑:

最后一个代码是:

  int power(int x, unsigned int y)
  {
     int temp;
     if( y == 0)
       return 1;
     if( y ==1)
        return x;
     temp = power(x, y/2);
     if (y%2 == 0)
       return temp*temp;
     else
       return x*temp*temp;
  }

y==1
时,由于没有提前退出,因此有多余的倍数

y==1
时,执行最后一行:

return x*temp*temp;
这简化为:

return x*1*1;

y==1
添加一个特例将消除额外的2个乘法。

y==1
时,由于没有提前退出,因此有多余的乘法

y==1
时,执行最后一行:

return x*temp*temp;
这简化为:

return x*1*1;

y==1添加一个特例将消除额外的2个乘法。

您省略了

 if (y==1)
     return x
而是需要从

 temp = power(x, 0)
 return x * temp * temp

额外的一对乘法来自不必要的最终递归调用。

您忽略了

 if (y==1)
     return x
int power(int x, unsigned int y)
  {
     int temp;
     if( y ==1)
        return x;

     if (y%2 == 0){
      temp = power(x, y/2);
       return temp*temp;
     }
     else{
       temp = power(x, (y-1)/2);
       return x*temp*temp;
     }
  }
而是需要从

 temp = power(x, 0)
 return x * temp * temp

额外的一对乘法来自不必要的最终递归调用。

使用分而治之策略的最佳方法。完成执行需要O(logn)时间。它也适用于负指数

int power(int x, unsigned int y)
  {
     int temp;
     if( y ==1)
        return x;

     if (y%2 == 0){
      temp = power(x, y/2);
       return temp*temp;
     }
     else{
       temp = power(x, (y-1)/2);
       return x*temp*temp;
     }
  }
我用C++做这个:

#include <iostream>
using namespace std;

float power(int a, int b)
{
    if (b == 0)
    {
        return 1;
    }
    else
    {
        float temp = power(a, b / 2);
        if (b > 0)
        {
            if (b % 2 == 0)
            {
                return temp * temp;
            }
            else
            {
                return a * temp * temp;
            }
        }
        else
        {
            return 1/power(a,-b);
        }
    }
}
int main()
{       int a , b ;
        cout<<"Enter a Number:";cin>>a; cout<<"Enter its exponential:";cin>>b;
        cout << power(a, b);
}
#包括
使用名称空间std;
浮动功率(内部a、内部b)
{
如果(b==0)
{
返回1;
}
其他的
{
浮子温度=功率(a、b/2);
如果(b>0)
{
如果(b%2==0)
{
返回温度*温度;
}
其他的
{
返回一个*温度*温度;
}
}
其他的
{
返回1/功率(a,-b);
}
}
}
int main()
{int a,b;
库塔;

使用分而治之的策略是最好的方法。完成执行需要O(logn)时间。它也适用于负指数

我用C++做这个:

#include <iostream>
using namespace std;

float power(int a, int b)
{
    if (b == 0)
    {
        return 1;
    }
    else
    {
        float temp = power(a, b / 2);
        if (b > 0)
        {
            if (b % 2 == 0)
            {
                return temp * temp;
            }
            else
            {
                return a * temp * temp;
            }
        }
        else
        {
            return 1/power(a,-b);
        }
    }
}
int main()
{       int a , b ;
        cout<<"Enter a Number:";cin>>a; cout<<"Enter its exponential:";cin>>b;
        cout << power(a, b);
}
#包括
使用名称空间std;
浮动功率(内部a、内部b)
{
如果(b==0)
{
返回1;
}
其他的
{
浮子温度=功率(a、b/2);
如果(b>0)
{
如果(b%2==0)
{
返回温度*温度;
}
其他的
{
返回一个*温度*温度;
}
}
其他的
{
返回1/功率(a,-b);
}
}
}
int main()
{int a,b;
库塔;


8是从哪里来的?我想你需要深入研究。为什么你需要一直到y==0?当y==1或2时停止。它可以保存一些乘法。顺便说一句,如果你意识到效率的话,使用
y&0x1==0
。@OliCharlesworth重复平方,32+8+4+2=46@HuStmpHrrr一个好的编译器会为你做这样的替换。顺便说一句,我不认为这算是分而治之,只是基本的递归。8从何而来?我想你要深入。为什么你需要继续到y==0?当y==1或2时停止。它可以节省一些乘法。顺便说一句,如果你意识到效率的话,使用
y&0x1==0
。@OliCharlesworth重复平方,32+8+4+2=46@HuStmpHrrr一个好的编译器会顺便说一句。我不认为这算是分而治之,只是基本的递归。亲爱的切佩纳,请你解释一下好吗?当
y==1
时,你可以立即返回
x
,因为
x^1==x
对于所有
x
。你不需要先递归地计算
x^0
,然后返回
 x*1*1
。请您更改我的代码并添加到您的答案中好吗?您只需要为您的函数添加另一个基本情况。亲爱的Chepener,请您进一步解释一下好吗?当
y==1
时,您可以立即返回
x
,因为
x^1==x
对于所有
x
。您不需要首先计算
x^0
重复rsively,然后返回
x*1*1
。请更改我的代码并添加到您的答案中好吗?您只需要在函数中添加另一个基本情况。