算法通过除了模拟乘法 [英] algorithm to simulate multiplication by addition
本文介绍了算法通过除了模拟乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何设计一个算法加入到模拟乘法。输入两个整数。它们可以是零,正或负..
解决方案
高清乘以(A,B):
如果(一个== 1):
回复B
ELIF(一== 0):
返回0
elif的(一个℃,):
返回-multiply(-A,B)
其他:
回报B +乘法(A - 1,B)
How to design an algorithm to simulate multiplication by addition. input two integers. they may be zero, positive or negative..
解决方案
def multiply(a, b):
if (a == 1):
return b
elif (a == 0):
return 0
elif (a < 0):
return -multiply(-a, b)
else:
return b + multiply(a - 1, b)
这篇关于算法通过除了模拟乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文