Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
阿克曼函数已尝试通过以下代码实现
def A(m, n): if m == 0: return n + 1 elif m > 0 and n == 1: A(m - 1, 1) elif m > 0 and n > 0: A(m - 1, A(m, n - 1)) print A(4, 5)
您的函数不会为if语句的 3 个分支中的 2 个返回任何内容;仅当m == 0您明确返回一个值时。
if
m == 0
您还需要返回递归调用的结果:
def A(m, n): if m == 0: return n + 1 elif m > 0 and n == 1: return A(m - 1, 1) elif m > 0 and n > 0: return A(m - 1, A(m, n - 1))
如果没有显式返回,则函数以默认返回值结束None。
None