-1
我在算法教科書中看到了這個。我對中間遞歸函數感到困惑。如果你可以用一個例子來解釋它,比如4/2,那就太棒了!任何人都可以解釋這種除法算法的工作原理嗎?
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(floor(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)