0

谁能告诉我为什么此代码返回RuntimeError: maximum recursion depth exceeded

def maximum(tab, indDeb, indFin):
    if (indDeb == indFin):
        return indDeb
    else:
        m = (indDeb + indFin) / 2
        print "m = ", m
        k1 = maximum(tab, indDeb, indFin)
        k2 = maximum(tab, m+1, indFin)
        if (tab[k1] > tab[k2]):
            return k1
        else:
            return k2

if __name__ == "__main__":
    maximum([1, 2, 3, 4, 5], 0, 4)
4

2 回答 2

6

maximum您内部递归调用函数而不更改参数:

def maximum(tab, indDeb, indFin):
    # ...

    k1 = maximum(tab, indDeb, indFin)

这不能不导致无限递归。

也许您想限制上限索引:

k1 = maximum(tab, indDeb, m)
于 2013-10-03T00:09:10.523 回答
5

该行:

k1 = maximum(tab, indDeb, indFin)

应该:

k1 = maximum(tab, indDeb, m)
于 2013-10-03T00:09:54.083 回答