我正在关注 Skiena 的算法书。在第 1 章中,给出了不使用 / 或 * 运算符来除实数的问题。我在python中实现了这样的:
def main(dividend, divisor ):
print "dividend : ",dividend
print "divisor : ", divisor
if dividend<divisor:
print "dividend has to be greater than divisor"
return;
quotient=0
sum=0
while sum<dividend:
sum = sum+divisor
print "sum : ",sum
quotient = quotient+1
if sum>dividend:
remainder = dividend-(sum-divisor)
quotient=quotient-1
print "remainder : ", remainder
print "quotient :", quotient
if __name__ == "__main__":
# pass any two real number as dividend and divisor
main(29, 3)
该问题还指出,您必须“找到最快的方法”。有没有其他方法可以更快地解决问题?