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
Algorithm Sanjoy Dasgupta中提供的除法算法_Algorithm_Division - Fatal编程技术网

Algorithm Sanjoy Dasgupta中提供的除法算法

Algorithm Sanjoy Dasgupta中提供的除法算法,algorithm,division,Algorithm,Division,我正在读Sanjoy Dasgupta的《算法》一书中的除法算法。下面提到除法算法 function divide(x,y) Input: Two n-bit integers x and y, where y ≥ 1 Output: The quotient and remainder of x divided by y if x = 0: return (q,r) = (0,0) (q,r) = divide(x/2,y) q = 2·q, r = 2·r if x is odd: r

我正在读Sanjoy Dasgupta的《算法》一书中的除法算法。下面提到除法算法

function divide(x,y) 
Input: Two n-bit integers x and y, where y ≥ 1 
Output: The quotient and remainder of x divided by y
if x = 0: return (q,r) = (0,0)
(q,r) = divide(x/2,y)
q = 2·q, r = 2·r
if x is odd: r = r + 1 
if r ≥ y: r = r−y, q = q + 1
return (q,r)
我对上述算法的问题如下:

  • 我们如何用简单的术语编写上述算法的递归公式,这是书中缺少的,而我无法编写
  • 如果x是奇数,为什么我们要执行r=r+1
  • 为什么洞q=2.q和r=2.r

  • 感谢

    乘以2以补偿之前除以2的结果。
    如何编写递归公式
    我觉得所介绍的过程是递归的-您能详细说明一下使用
    递归
    得到的结果吗<代码>为什么dong q=2.q和r=2.r(我们在做什么?)正如在
    q=2.q,r=2.r
    的目的是什么?