Recursiveversion(递归版本)

    xiaoxiao2021-03-26  9

    • An alternative is to think of this computation as:

    a * b = a + a + … + a    (b 次相加)

            = a + a + … + a    (a 与b-1次的a相加)

            = a + a*(b-1)

    Recursion(递归)

    • This is an instance of a recursive algorithm

    – Reduce a problem to a simpler (or smaller) version of the same problem, plus some simple computations

            • Recursive step(递归步骤)

    – Keep reducing until reach a simple case that can besolved directly

            • Base case(基线条件)

    • a * b = a; if b = 1 (Base case)

    • a * b = a + a * (b-1); otherwise (Recursive case)

    def iterMul(a,b): if b == 1: return a else: return a + iterMul(a,b-1)

    转载请注明原文地址: https://ju.6miu.com/read-500032.html

    最新回复(0)