Algorithm 模拟加法乘法的算法

Algorithm 模拟加法乘法的算法,algorithm,multiplication,addition,Algorithm,Multiplication,Addition,如何设计模拟加法乘法的算法。输入两个整数。它们可以是零、正或负。某些伪代码: function multiply(x, y) if abs(x) = x and abs(y) = y or abs(x) <> x and abs(y) <> y then sign = 'plus' if abs(x) = x and abs(y) <> y or abs(x) <> x and abs(y) = y then sign = 'minus'

如何设计模拟加法乘法的算法。输入两个整数。它们可以是零、正或负。

某些伪代码:

function multiply(x, y)
  if abs(x) = x and abs(y) = y or abs(x) <> x and abs(y) <> y then sign = 'plus'
  if abs(x) = x and abs(y) <> y or abs(x) <> x and abs(y) = y then sign = 'minus'

 res = 0
 for i = 0 to abs(y)
  res = res + abs(x)
 end

 if sign = 'plus' return res
    else return -1 * res

end function
function mul(n, x)
    if n < 0 then   # 'n' cannot be negative
        n := -n
        x := -x
    endif

    y := 0
    while n != 0 do
        if n % 2 == 0 then
            x := x << 1     # x := x + x
            n := n >> 1     # n := n / 2
        else
            y := y + x
            x := x << 1     # x := x + x
            n := n - 1      # n := (n-1)/2
            n := n >> 1
        endif
    endwhile

    return y                # y = n * x
end
function is_even(n)
    n_original := n
    n := n >> 1        # n := n / 2
    n := n << 1        # n := n * 2
    if n = n_original then
        return true    # n is even
    else
        return false   # n is not even
    endif
end
函数乘法(x,y)
如果abs(x)=x和abs(y)=y或abs(x)x和abs(y)y,则符号='plus'
如果abs(x)=x和abs(y)y或abs(x)x和abs(y)=y,则符号='减'
res=0
对于i=0至abs(y)
res=res+abs(x)
结束
如果符号='plus'返回res
否则返回-1*res
端函数
一些伪代码:

function multiply(x, y)
  if abs(x) = x and abs(y) = y or abs(x) <> x and abs(y) <> y then sign = 'plus'
  if abs(x) = x and abs(y) <> y or abs(x) <> x and abs(y) = y then sign = 'minus'

 res = 0
 for i = 0 to abs(y)
  res = res + abs(x)
 end

 if sign = 'plus' return res
    else return -1 * res

end function
function mul(n, x)
    if n < 0 then   # 'n' cannot be negative
        n := -n
        x := -x
    endif

    y := 0
    while n != 0 do
        if n % 2 == 0 then
            x := x << 1     # x := x + x
            n := n >> 1     # n := n / 2
        else
            y := y + x
            x := x << 1     # x := x + x
            n := n - 1      # n := (n-1)/2
            n := n >> 1
        endif
    endwhile

    return y                # y = n * x
end
function is_even(n)
    n_original := n
    n := n >> 1        # n := n / 2
    n := n << 1        # n := n * 2
    if n = n_original then
        return true    # n is even
    else
        return false   # n is not even
    endif
end
函数乘法(x,y)
如果abs(x)=x和abs(y)=y或abs(x)x和abs(y)y,则符号='plus'
如果abs(x)=x和abs(y)y或abs(x)x和abs(y)=y,则符号='减'
res=0
对于i=0至abs(y)
res=res+abs(x)
结束
如果符号='plus'返回res
否则返回-1*res
端函数
def乘法运算(a、b):
如果(a==1):
返回b
elif(a==0):
返回0
elif(a<0):
返回-乘法(-a,b)
其他:
返回b+乘法(a-1,b)
def乘法运算(a、b):
如果(a==1):
返回b
elif(a==0):
返回0
elif(a<0):
返回-乘法(-a,b)
其他:
返回b+乘法(a-1,b)
val:=0
bothNegative:=假
if(输入1<0)和if(输入2<0)
bothNegative=true
if(双阴性)
较小值:=绝对值(较小值)
对于[i:=绝对值(更大的数值);i!=0;i--]
do val+=较小的数值
返回val;
val:=0
bothNegative:=假
if(输入1<0)和if(输入2<0)
bothNegative=true
if(双阴性)
较小值:=绝对值(较小值)
对于[i:=绝对值(更大的数值);i!=0;i--]
do val+=较小的数值
返回val;
mul(a,b)
{
sign1=sign2=1;
如果(a==0 | | b==0)
返回0;
如果(a
mul(a,b)
{
sign1=sign2=1;
如果(a==0 | | b==0)
返回0;

如果(a对于整数,这是怎么回事

int multiply(int a, int b)
{
    int product = 0;
    int i;
    if ( b > 0 )
    {
        for(i = 0; i < b ; i++)
        {
            product += a;
        }
    }
    else
    {
        for(i = 0; i > b ; i--)
        {
            product -= a;
        }
    }

    return product;
}
intmultiply(inta,intb)
{
int乘积=0;
int i;
如果(b>0)
{
对于(i=0;ib;i--)
{
产品-=a;
}
}
退货产品;
}

对于整数,这是怎么回事:

int multiply(int a, int b)
{
    int product = 0;
    int i;
    if ( b > 0 )
    {
        for(i = 0; i < b ; i++)
        {
            product += a;
        }
    }
    else
    {
        for(i = 0; i > b ; i--)
        {
            product -= a;
        }
    }

    return product;
}
intmultiply(inta,intb)
{
int乘积=0;
int i;
如果(b>0)
{
对于(i=0;ib;i--)
{
产品-=a;
}
}
退货产品;
}

我之所以来到这里,是因为我在寻找乘法算法,而没有使用
*
运算。我在这里看到的只是n次数的加减。它是O(n),没问题,但是

如果有
按位移位
运算,则可以得到O(logn)乘法算法。 这是我的伪代码:

function multiply(x, y)
  if abs(x) = x and abs(y) = y or abs(x) <> x and abs(y) <> y then sign = 'plus'
  if abs(x) = x and abs(y) <> y or abs(x) <> x and abs(y) = y then sign = 'minus'

 res = 0
 for i = 0 to abs(y)
  res = res + abs(x)
 end

 if sign = 'plus' return res
    else return -1 * res

end function
function mul(n, x)
    if n < 0 then   # 'n' cannot be negative
        n := -n
        x := -x
    endif

    y := 0
    while n != 0 do
        if n % 2 == 0 then
            x := x << 1     # x := x + x
            n := n >> 1     # n := n / 2
        else
            y := y + x
            x := x << 1     # x := x + x
            n := n - 1      # n := (n-1)/2
            n := n >> 1
        endif
    endwhile

    return y                # y = n * x
end
function is_even(n)
    n_original := n
    n := n >> 1        # n := n / 2
    n := n << 1        # n := n * 2
    if n = n_original then
        return true    # n is even
    else
        return false   # n is not even
    endif
end
使用
按位和


我来这里是因为我在寻找乘法算法,没有使用
*
运算。我在这里看到的只是n次加减数。它是O(n),这是可以的,但是

如果有
按位移位
运算,则可以得到O(logn)乘法算法。 这是我的伪代码:

function multiply(x, y)
  if abs(x) = x and abs(y) = y or abs(x) <> x and abs(y) <> y then sign = 'plus'
  if abs(x) = x and abs(y) <> y or abs(x) <> x and abs(y) = y then sign = 'minus'

 res = 0
 for i = 0 to abs(y)
  res = res + abs(x)
 end

 if sign = 'plus' return res
    else return -1 * res

end function
function mul(n, x)
    if n < 0 then   # 'n' cannot be negative
        n := -n
        x := -x
    endif

    y := 0
    while n != 0 do
        if n % 2 == 0 then
            x := x << 1     # x := x + x
            n := n >> 1     # n := n / 2
        else
            y := y + x
            x := x << 1     # x := x + x
            n := n - 1      # n := (n-1)/2
            n := n >> 1
        endif
    endwhile

    return y                # y = n * x
end
function is_even(n)
    n_original := n
    n := n >> 1        # n := n / 2
    n := n << 1        # n := n * 2
    if n = n_original then
        return true    # n is even
    else
        return false   # n is not even
    endif
end
使用
按位和


问题是在不使用乘法函数的情况下模拟乘法……您可以只使用加法……从技术上讲,您不需要检查a==1的情况。@enjay:因为没有使用实际的乘法,所以不需要为a==1提前退出。如果a==1,则调用else,b+乘法(0,b)->b+0将被返回,一切正常。因此,不需要a==1的情况。@Saurabh:“-”处的乘法符号可以很容易地被写0…-代替。@ Surabh这是我递归思考的准则:1。考虑所有的基本情况。2。递归调用必须在一个较小的元素或子集上进行。3。相信递归调用的正确性。如果你知道你已经考虑了所有的基本情况,并且你已经考虑了ACTTL。你要为递归步骤做些什么,那么递归就可以完成了。递归是一个棘手的事情。你一旦理解它并使用它,它会使你的问题概念化得更容易,因为你可以把每个部分都考虑在内。问题是关于模拟乘法。使用乘法函数…您可以只使用加法..从技术上讲,您不需要检查a==1的情况。@enjay:因为没有使用实际的乘法,所以不需要提前对a==1进行解救。如果a==1,则调用else,b+乘法(0,b)->b+0将被返回,一切正常。因此,不需要a==1的情况。@Saurabh:“-”处的乘法符号可以很容易地被写0…-代替。@ Surabh这是我递归思考的准则:1。考虑所有的基本情况。2。递归调用必须在一个较小的元素或子集上进行。3。相信递归调用的正确性。如果你知道你已经考虑了所有的基本情况,并且你已经考虑了ACTTL。你要为递归步骤做些什么,那么递归就可以完成了。递归是一个棘手的事情,从头开始把它包起来。一旦你理解了它并多使用它,它会使你的问题概念化得更容易,因为你可以单独考虑每一个部分。这是一个非常简单的解决方案。n、 p