{"id":1020105,"date":"2024-12-27T13:08:15","date_gmt":"2024-12-27T05:08:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1020105.html"},"modified":"2024-12-27T13:08:17","modified_gmt":"2024-12-27T05:08:17","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%bf%90%e7%94%a8%e9%80%92%e5%bd%92","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1020105.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8fd0\u7528\u9012\u5f52"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25162521\/4fcaac7d-d6e1-4d20-a6d3-50bc4fa2997c.webp\" alt=\"python\u4e2d\u5982\u4f55\u8fd0\u7528\u9012\u5f52\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u9012\u5f52\u662f\u4e00\u79cd\u5728\u51fd\u6570\u4e2d\u8c03\u7528\u81ea\u8eab\u7684\u7f16\u7a0b\u6280\u672f<\/strong>\uff0c\u5b83\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u53ef\u4ee5\u88ab\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u3001\u76f8\u4f3c\u95ee\u9898\u7684\u95ee\u9898\u3002<strong>\u9012\u5f52\u7684\u5173\u952e\u662f\u5b9a\u4e49\u4e00\u4e2a\u57fa\u51c6\u6761\u4ef6\u6765\u7ec8\u6b62\u9012\u5f52\u3001\u786e\u4fdd\u6bcf\u6b21\u9012\u5f52\u8c03\u7528\u90fd\u5c06\u95ee\u9898\u89c4\u6a21\u7f29\u5c0f<\/strong>\u3002\u9012\u5f52\u5728\u5904\u7406\u6811\u5f62\u7ed3\u6784\u3001\u6392\u5217\u7ec4\u5408\u3001\u52a8\u6001\u89c4\u5212\u7b49\u95ee\u9898\u4e2d\u975e\u5e38\u6709\u7528\u3002\u9012\u5f52\u7684\u5b9e\u9645\u5e94\u7528\u5305\u62ec\u8ba1\u7b97\u9636\u4e58\u3001\u6590\u6ce2\u90a3\u5951\u6570\u5217\u3001\u6c49\u8bfa\u5854\u95ee\u9898\u7b49\u3002<strong>\u6700\u91cd\u8981\u7684\u662f\u786e\u4fdd\u9012\u5f52\u80fd\u6b63\u786e\u7ec8\u6b62\uff0c\u4ee5\u907f\u514d\u65e0\u9650\u9012\u5f52\u5bfc\u81f4\u7684\u6808\u6ea2\u51fa<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u9012\u5f52\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9012\u5f52\u7684\u57fa\u672c\u6982\u5ff5\u548c\u539f\u7406<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u662f\u4e00\u79cd\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\uff0c\u5176\u4e2d\u95ee\u9898\u88ab\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u540c\u7c7b\u95ee\u9898\u3002\u9012\u5f52\u6700\u57fa\u672c\u7684\u5f62\u5f0f\u662f\u4e00\u4e2a\u51fd\u6570\u76f4\u63a5\u8c03\u7528\u81ea\u8eab\u3002\u9012\u5f52\u51fd\u6570\u7531\u4e24\u90e8\u5206\u7ec4\u6210\uff1a\u57fa\u51c6\u6761\u4ef6\u548c\u9012\u5f52\u6b65\u9aa4\u3002\u57fa\u51c6\u6761\u4ef6\u662f\u7528\u4e8e\u7ec8\u6b62\u9012\u5f52\u7684\u6761\u4ef6\uff0c\u5f53\u6ee1\u8db3\u65f6\uff0c\u51fd\u6570\u4e0d\u518d\u8c03\u7528\u81ea\u8eab\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u9012\u5f52\u6b65\u9aa4\u662f\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u90e8\u5206\uff0c\u5e76\u4e14\u901a\u5e38\u662f\u5728\u95ee\u9898\u89c4\u6a21\u7f29\u5c0f\u7684\u60c5\u51b5\u4e0b\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u7684\u8c03\u7528\u6808\u662f\u9012\u5f52\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\u3002\u5728\u6bcf\u6b21\u8c03\u7528\u9012\u5f52\u51fd\u6570\u65f6\uff0c\u5f53\u524d\u7684\u51fd\u6570\u6267\u884c\u88ab\u6302\u8d77\uff0c\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u548c\u53c2\u6570\u88ab\u4fdd\u5b58\u5728\u8c03\u7528\u6808\u4e2d\u3002\u5f53\u9012\u5f52\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u7a0b\u5e8f\u4ece\u8c03\u7528\u6808\u4e2d\u5f39\u51fa\u4e4b\u524d\u4fdd\u5b58\u7684\u72b6\u6001\uff0c\u7ee7\u7eed\u6267\u884c\u3002\u7406\u89e3\u8c03\u7528\u6808\u6709\u52a9\u4e8e\u5206\u6790\u9012\u5f52\u7684\u5de5\u4f5c\u539f\u7406\u53ca\u5176\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898\uff0c\u5982\u6808\u6ea2\u51fa\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u9012\u5f52\u7684\u57fa\u672c\u5b9e\u73b0<\/p>\n<\/p>\n<p><p>\u5b9e\u73b0\u9012\u5f52\u51fd\u6570\u9700\u8981\u660e\u786e\u57fa\u51c6\u6761\u4ef6\u548c\u9012\u5f52\u6b65\u9aa4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u9012\u5f52\u51fd\u6570\u793a\u4f8b\uff1a\u8ba1\u7b97\u4e00\u4e2a\u6574\u6570\u7684\u9636\u4e58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    if n == 0:<\/p>\n<p>        return 1  # \u57fa\u51c6\u6761\u4ef6<\/p>\n<p>    else:<\/p>\n<p>        return n * factorial(n - 1)  # \u9012\u5f52\u6b65\u9aa4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u57fa\u51c6\u6761\u4ef6\u662f\u5f53 <code>n<\/code> \u7b49\u4e8e 0 \u65f6\uff0c\u8fd4\u56de 1\u3002\u8fd9\u662f\u56e0\u4e3a 0 \u7684\u9636\u4e58\u5b9a\u4e49\u4e3a 1\u3002\u9012\u5f52\u6b65\u9aa4\u662f <code>n * factorial(n - 1)<\/code>\uff0c\u5373\u5f53\u524d\u503c\u4e58\u4ee5 <code>n<\/code> \u51cf 1 \u7684\u9636\u4e58\u3002<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u7684\u6b63\u786e\u6027\u5728\u4e8e\u6bcf\u6b21\u9012\u5f52\u8c03\u7528\u90fd\u7f29\u5c0f\u4e86\u95ee\u9898\u89c4\u6a21\uff0c\u6700\u7ec8\u5c06\u8fbe\u5230\u57fa\u51c6\u6761\u4ef6\u3002\u786e\u4fdd\u57fa\u51c6\u6761\u4ef6\u5b58\u5728\u5e76\u4e14\u80fd\u591f\u8fbe\u5230\u662f\u907f\u514d\u9012\u5f52\u9677\u5165\u65e0\u9650\u5faa\u73af\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u9012\u5f52\u7684\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li><strong>\u6590\u6ce2\u90a3\u5951\u6570\u5217<\/strong><\/li>\n<\/ol>\n<p><p>\u6590\u6ce2\u90a3\u5951\u6570\u5217\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u9012\u5f52\u95ee\u9898\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6570\u662f\u524d\u4e24\u4e2a\u6570\u7684\u548c\u3002\u9012\u5f52\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fibonacci(n):<\/p>\n<p>    if n &lt;= 1:<\/p>\n<p>        return n  # \u57fa\u51c6\u6761\u4ef6<\/p>\n<p>    else:<\/p>\n<p>        return fibonacci(n - 1) + fibonacci(n - 2)  # \u9012\u5f52\u6b65\u9aa4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c3d\u7ba1\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\uff0c\u4f46\u6548\u7387\u8f83\u4f4e\uff0c\u56e0\u4e3a\u5b83\u91cd\u590d\u8ba1\u7b97\u4e86\u8bb8\u591a\u76f8\u540c\u7684\u5b50\u95ee\u9898\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6c49\u8bfa\u5854\u95ee\u9898<\/strong><\/li>\n<\/ol>\n<p><p>\u6c49\u8bfa\u5854\u95ee\u9898\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u9012\u5f52\u95ee\u9898\uff0c\u6d89\u53ca\u5c06\u4e00\u7ec4\u5706\u76d8\u4ece\u4e00\u4e2a\u67f1\u5b50\u79fb\u52a8\u5230\u53e6\u4e00\u4e2a\u67f1\u5b50\u3002\u9012\u5f52\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def hanoi(n, source, target, auxiliary):<\/p>\n<p>    if n &gt; 0:<\/p>\n<p>        hanoi(n - 1, source, auxiliary, target)<\/p>\n<p>        print(f&quot;Move disk {n} from {source} to {target}&quot;)<\/p>\n<p>        hanoi(n - 1, auxiliary, target, source)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5b9e\u73b0\u5c55\u793a\u4e86\u9012\u5f52\u5982\u4f55\u6709\u6548\u5730\u89e3\u51b3\u590d\u6742\u7684\u95ee\u9898\uff0c\u901a\u8fc7\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9012\u5f52\u7684\u4f18\u7f3a\u70b9<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u4ee3\u7801\u7b80\u6d01\u548c\u903b\u8f91\u6e05\u6670\uff0c\u7279\u522b\u9002\u5408\u89e3\u51b3\u5177\u6709\u81ea\u76f8\u4f3c\u6027\u8d28\u7684\u95ee\u9898\u3002\u7136\u800c\uff0c\u9012\u5f52\u4e5f\u6709\u5176\u7f3a\u70b9\uff0c\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6548\u7387\u95ee\u9898<\/strong>\uff1a\u9012\u5f52\u53ef\u80fd\u5bfc\u81f4\u5927\u91cf\u7684\u91cd\u590d\u8ba1\u7b97\uff0c\u7279\u522b\u662f\u5728\u6ca1\u6709\u4f7f\u7528\u7f13\u5b58\u6280\u672f\uff08\u5982\u8bb0\u5fc6\u5316\uff09\u7684\u60c5\u51b5\u4e0b\u3002<\/li>\n<li><strong>\u6808\u7a7a\u95f4\u9650\u5236<\/strong>\uff1a\u6bcf\u6b21\u9012\u5f52\u8c03\u7528\u90fd\u4f1a\u6d88\u8017\u6808\u7a7a\u95f4\uff0c\u8fc7\u591a\u7684\u9012\u5f52\u8c03\u7528\u53ef\u80fd\u5bfc\u81f4\u6808\u6ea2\u51fa\u3002<\/li>\n<\/ul>\n<p><p>\u4e94\u3001\u4f18\u5316\u9012\u5f52\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bb0\u5fc6\u5316<\/strong><\/li>\n<\/ol>\n<p><p>\u8bb0\u5fc6\u5316\u662f\u901a\u8fc7\u7f13\u5b58\u9012\u5f52\u8c03\u7528\u7684\u7ed3\u679c\u6765\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u7684\u4e00\u79cd\u4f18\u5316\u6280\u672f\u3002Python \u4e2d\u53ef\u4ee5\u4f7f\u7528 <code>functools.lru_cache<\/code> \u6765\u5b9e\u73b0\u8bb0\u5fc6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import lru_cache<\/p>\n<p>@lru_cache(maxsize=None)<\/p>\n<p>def fibonacci(n):<\/p>\n<p>    if n &lt;= 1:<\/p>\n<p>        return n<\/p>\n<p>    else:<\/p>\n<p>        return fibonacci(n - 1) + fibonacci(n - 2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u4f18\u5316\u6280\u672f\u663e\u8457\u63d0\u9ad8\u4e86\u9012\u5f52\u51fd\u6570\u7684\u6027\u80fd\uff0c\u5c24\u5176\u5728\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u65f6\u6548\u679c\u663e\u8457\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8fed\u4ee3\u66ff\u4ee3<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u7684\u65b9\u6cd5\u66ff\u4ee3\u9012\u5f52\uff0c\u4ee5\u907f\u514d\u6808\u6ea2\u51fa\u7684\u95ee\u9898\u3002\u9636\u4e58\u7684\u8fed\u4ee3\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial_iterative(n):<\/p>\n<p>    result = 1<\/p>\n<p>    for i in range(2, n + 1):<\/p>\n<p>        result *= i<\/p>\n<p>    return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u9012\u5f52\u5728\u590d\u6742\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u5728\u5904\u7406\u6811\u5f62\u548c\u56fe\u5f62\u6570\u636e\u7ed3\u6784\u65f6\u5c24\u4e3a\u5f3a\u5927\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e8c\u53c9\u6811\u904d\u5386<\/strong><\/li>\n<\/ol>\n<p><p>\u9012\u5f52\u5728\u6811\u7684\u904d\u5386\u4e2d\u5e94\u7528\u5e7f\u6cdb\uff0c\u5305\u62ec\u524d\u5e8f\u3001\u4e2d\u5e8f\u548c\u540e\u5e8f\u904d\u5386\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class TreeNode:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>        self.left = None<\/p>\n<p>        self.right = None<\/p>\n<p>def inorder_traversal(node):<\/p>\n<p>    if node:<\/p>\n<p>        inorder_traversal(node.left)<\/p>\n<p>        print(node.value)<\/p>\n<p>        inorder_traversal(node.right)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u9012\u5f52\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u5b9e\u73b0\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u7684\u904d\u5386\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u641c\u7d22<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u56fe\u7684\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff08DFS\uff09\u4e2d\uff0c\u9012\u5f52\u53ef\u4ee5\u7528\u6765\u63a2\u7d22\u56fe\u4e2d\u7684\u6240\u6709\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def dfs(graph, start, visited=None):<\/p>\n<p>    if visited is None:<\/p>\n<p>        visited = set()<\/p>\n<p>    visited.add(start)<\/p>\n<p>    print(start)<\/p>\n<p>    for neighbor in graph[start]:<\/p>\n<p>        if neighbor not in visited:<\/p>\n<p>            dfs(graph, neighbor, visited)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9012\u5f52\u5728\u56fe\u7684\u904d\u5386\u4e2d\u540c\u6837\u8868\u73b0\u51fa\u8272\uff0c\u80fd\u591f\u6709\u6548\u5730\u5904\u7406\u5faa\u73af\u548c\u5206\u652f\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u9012\u5f52\u7684\u66ff\u4ee3\u65b9\u6848\u4e0e\u6bd4\u8f83<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9012\u5f52\u53ef\u4ee5\u88ab\u5176\u4ed6\u65b9\u6cd5\u66ff\u4ee3\uff0c\u5982\u8fed\u4ee3\u548c\u52a8\u6001\u89c4\u5212\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u52a8\u6001\u89c4\u5212<\/strong><\/li>\n<\/ol>\n<p><p>\u52a8\u6001\u89c4\u5212\u662f\u4e00\u79cd\u901a\u8fc7\u5b58\u50a8\u5b50\u95ee\u9898\u7684\u7ed3\u679c\u6765\u51cf\u5c11\u8ba1\u7b97\u91cf\u7684\u65b9\u6cd5\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u4f18\u5316\u9012\u5f52\u7b97\u6cd5\uff0c\u7279\u522b\u662f\u5728\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fibonacci_dynamic(n):<\/p>\n<p>    fib = [0, 1]<\/p>\n<p>    for i in range(2, n + 1):<\/p>\n<p>        fib.append(fib[i - 1] + fib[i - 2])<\/p>\n<p>    return fib[n]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u52a8\u6001\u89c4\u5212\u901a\u8fc7\u4f7f\u7528\u6570\u7ec4\u5b58\u50a8\u4e2d\u95f4\u7ed3\u679c\uff0c\u907f\u514d\u4e86\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u9ad8\u4e86\u6548\u7387\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5c3e\u9012\u5f52\u4f18\u5316<\/strong><\/li>\n<\/ol>\n<p><p>\u5c3e\u9012\u5f52\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u9012\u5f52\u5f62\u5f0f\uff0c\u5176\u4e2d\u9012\u5f52\u8c03\u7528\u662f\u51fd\u6570\u7684\u6700\u540e\u4e00\u4e2a\u64cd\u4f5c\u3002\u67d0\u4e9b\u7f16\u7a0b\u8bed\u8a00\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u8fed\u4ee3\u5f62\u5f0f\u4ee5\u63d0\u9ad8\u6548\u7387\u3002\u7136\u800c\uff0cPython \u9ed8\u8ba4\u4e0d\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u9700\u8981\u624b\u52a8\u8f6c\u6362\u4e3a\u8fed\u4ee3\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def t<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l_recursive_factorial(n, accumulator=1):<\/p>\n<p>    if n == 0:<\/p>\n<p>        return accumulator<\/p>\n<p>    else:<\/p>\n<p>        return tail_recursive_factorial(n - 1, n * accumulator)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136 Python \u4e0d\u652f\u6301\u81ea\u52a8\u7684\u5c3e\u9012\u5f52\u4f18\u5316\uff0c\u4f46\u7406\u89e3\u8fd9\u4e00\u6982\u5ff5\u6709\u52a9\u4e8e\u7f16\u5199\u66f4\u9ad8\u6548\u7684\u9012\u5f52\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u9012\u5f52\u7684\u8c03\u8bd5\u4e0e\u6d4b\u8bd5<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u51fd\u6570\u7684\u8c03\u8bd5\u53ef\u80fd\u6bd4\u8f83\u56f0\u96be\uff0c\u56e0\u4e3a\u5b83\u4eec\u6d89\u53ca\u591a\u4e2a\u5d4c\u5957\u7684\u51fd\u6570\u8c03\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8c03\u8bd5\u9012\u5f52\u7684\u6280\u5de7\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5370\u8c03\u8bd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u6dfb\u52a0\u6253\u5370\u8bed\u53e5\u53ef\u4ee5\u5e2e\u52a9\u8ddf\u8e2a\u51fd\u6570\u7684\u6267\u884c\u8fc7\u7a0b\u3002\u6253\u5370\u5f53\u524d\u53c2\u6570\u548c\u8fd4\u56de\u503c\u80fd\u591f\u63d0\u4f9b\u5bf9\u9012\u5f52\u884c\u4e3a\u7684\u6df1\u5165\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def factorial(n):<\/p>\n<p>    print(f&quot;factorial({n}) called&quot;)<\/p>\n<p>    if n == 0:<\/p>\n<p>        return 1<\/p>\n<p>    else:<\/p>\n<p>        result = n * factorial(n - 1)<\/p>\n<p>        print(f&quot;factorial({n}) returns {result}&quot;)<\/p>\n<p>        return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u8c03\u8bd5\u5668<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\uff08\u5982 Python \u7684 <code>pdb<\/code>\uff09\u53ef\u4ee5\u9010\u6b65\u6267\u884c\u9012\u5f52\u51fd\u6570\uff0c\u67e5\u770b\u6bcf\u4e2a\u8c03\u7528\u7684\u5806\u6808\u4fe1\u606f\u548c\u53d8\u91cf\u72b6\u6001\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/li>\n<\/ol>\n<p><p>\u4e3a\u9012\u5f52\u51fd\u6570\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u786e\u4fdd\u5176\u6b63\u786e\u6027\u548c\u7a33\u5b9a\u6027\u3002\u901a\u8fc7\u6d4b\u8bd5\u5404\u79cd\u8f93\u5165\u548c\u8fb9\u754c\u6761\u4ef6\uff0c\u53ef\u4ee5\u9a8c\u8bc1\u9012\u5f52\u51fd\u6570\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>class TestFactorial(unittest.TestCase):<\/p>\n<p>    def test_factorial(self):<\/p>\n<p>        self.assertEqual(factorial(0), 1)<\/p>\n<p>        self.assertEqual(factorial(5), 120)<\/p>\n<p>        self.assertEqual(factorial(10), 3628800)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u9012\u5f52\u7684\u6027\u80fd\u4e0e\u590d\u6742\u5ea6\u5206\u6790<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u7684\u6027\u80fd\u5206\u6790\u6d89\u53ca\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\u7684\u8bc4\u4f30\u3002\u9012\u5f52\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u53d6\u51b3\u4e8e\u9012\u5f52\u8c03\u7528\u7684\u6b21\u6570\u548c\u6bcf\u6b21\u8c03\u7528\u7684\u8ba1\u7b97\u91cf\u3002\u7a7a\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e0e\u9012\u5f52\u6df1\u5ea6\u6709\u5173\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u9012\u5f52\u8c03\u7528\u90fd\u9700\u8981\u6808\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong><\/li>\n<\/ol>\n<p><p>\u9012\u5f52\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u6811\u6216\u9012\u5f52\u5173\u7cfb\u5f0f\u6765\u5206\u6790\u3002\u4f8b\u5982\uff0c\u6590\u6ce2\u90a3\u5951\u9012\u5f52\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(2^n)\uff0c\u800c\u901a\u8fc7\u8bb0\u5fc6\u5316\u6216\u52a8\u6001\u89c4\u5212\u4f18\u5316\u540e\u53ef\u964d\u4e3a O(n)\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7a7a\u95f4\u590d\u6742\u5ea6<\/strong><\/li>\n<\/ol>\n<p><p>\u9012\u5f52\u51fd\u6570\u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u7531\u8c03\u7528\u6808\u7684\u6df1\u5ea6\u51b3\u5b9a\u3002\u5728\u6ca1\u6709\u4f18\u5316\u7684\u60c5\u51b5\u4e0b\uff0c\u7a7a\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a O(n)\uff0c\u4f46\u901a\u8fc7\u5c3e\u9012\u5f52\u4f18\u5316\u6216\u8fed\u4ee3\u66ff\u4ee3\u53ef\u4ee5\u51cf\u5c11\u7a7a\u95f4\u6d88\u8017\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u9012\u5f52\u7684\u5c40\u9650\u6027\u4e0e\u672a\u6765\u53d1\u5c55<\/p>\n<\/p>\n<p><p>\u9012\u5f52\u5728\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u65f6\u975e\u5e38\u6709\u6548\uff0c\u4f46\u4e5f\u6709\u5176\u5c40\u9650\u6027\u3002\u9012\u5f52\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u6df1\u5ea6\u5f88\u5927\u7684\u60c5\u51b5\u4e0b\u3002\u6b64\u5916\uff0c\u9012\u5f52\u53ef\u80fd\u4e0d\u6613\u7406\u89e3\u548c\u8c03\u8bd5\uff0c\u5c24\u5176\u5bf9\u65b0\u624b\u7f16\u7a0b\u8005\u800c\u8a00\u3002<\/p>\n<\/p>\n<p><p>\u968f\u7740\u7f16\u7a0b\u8bed\u8a00\u7684\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u8bed\u8a00\u548c\u7f16\u8bd1\u5668\u652f\u6301\u5c3e\u9012\u5f52\u4f18\u5316\u548c\u5176\u4ed6\u9012\u5f52\u4f18\u5316\u6280\u672f\u3002\u672a\u6765\uff0c\u9012\u5f52\u7684\u6548\u7387\u548c\u53ef\u7528\u6027\u53ef\u80fd\u4f1a\u5f97\u5230\u8fdb\u4e00\u6b65\u63d0\u5347\u3002\u6b64\u5916\uff0c\u65b0\u7684\u7f16\u7a0b\u8303\u5f0f\u548c\u7b97\u6cd5\u53ef\u80fd\u4f1a\u5e26\u6765\u9012\u5f52\u7684\u66f4\u591a\u5e94\u7528\u573a\u666f\u548c\u4f18\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a\u9012\u5f52\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u5728\u89e3\u51b3\u7279\u5b9a\u95ee\u9898\u65f6\u975e\u5e38\u6709\u6548\u3002\u4e86\u89e3\u9012\u5f52\u7684\u57fa\u672c\u539f\u7406\u3001\u5e94\u7528\u573a\u666f\u3001\u4f18\u7f3a\u70b9\u4ee5\u53ca\u4f18\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u5229\u7528\u8fd9\u4e00\u6280\u672f\u3002\u901a\u8fc7\u8c03\u8bd5\u548c\u6d4b\u8bd5\uff0c\u786e\u4fdd\u9012\u5f52\u51fd\u6570\u7684\u6b63\u786e\u6027\u548c\u6548\u7387\uff0c\u5e76\u6839\u636e\u5177\u4f53\u95ee\u9898\u9009\u62e9\u5408\u9002\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u9012\u5f52\u7684\u57fa\u672c\u6982\u5ff5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u9012\u5f52\u662f\u6307\u4e00\u4e2a\u51fd\u6570\u5728\u5176\u5b9a\u4e49\u4e2d\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u8c03\u7528\u81ea\u8eab\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u53ef\u4ee5\u5206\u89e3\u4e3a\u66f4\u7b80\u5355\u5b50\u95ee\u9898\u7684\u95ee\u9898\u3002\u9012\u5f52\u7531\u4e24\u4e2a\u4e3b\u8981\u90e8\u5206\u7ec4\u6210\uff1a\u57fa\u51c6\u6761\u4ef6\u548c\u9012\u5f52\u6761\u4ef6\u3002\u57fa\u51c6\u6761\u4ef6\u7528\u4e8e\u7ec8\u6b62\u9012\u5f52\uff0c\u800c\u9012\u5f52\u6761\u4ef6\u5219\u7528\u4e8e\u5c06\u95ee\u9898\u7b80\u5316\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u9012\u5f52\u51fd\u6570\uff1f<\/strong><br \/>\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u9012\u5f52\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u4e00\u4e2a\u8ba1\u7b97\u9636\u4e58\u7684\u51fd\u6570\u6765\u8bf4\u660e\u3002\u9636\u4e58\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u9012\u5f52\u95ee\u9898\uff0c\u53ef\u4ee5\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">def factorial(n):\n    if n == 0:\n        return 1  # \u57fa\u51c6\u6761\u4ef6\n    else:\n        return n * factorial(n - 1)  # \u9012\u5f52\u8c03\u7528\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>factorial<\/code>\u51fd\u6570\u4f1a\u4e0d\u65ad\u8c03\u7528\u81ea\u8eab\uff0c\u76f4\u5230\u8fbe\u5230\u57fa\u51c6\u6761\u4ef6<code>n == 0<\/code>\u3002<\/p>\n<p><strong>\u9012\u5f52\u5728Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1f<\/strong><br \/>\u9012\u5f52\u5728\u8bb8\u591a\u573a\u666f\u4e0b\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5904\u7406\u6811\u5f62\u6570\u636e\u7ed3\u6784\uff08\u5982\u6587\u4ef6\u7cfb\u7edf\u904d\u5386\uff09\u3001\u89e3\u51b3\u52a8\u6001\u89c4\u5212\u95ee\u9898\uff08\u5982\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff09\u3001\u4ee5\u53ca\u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b49\u3002\u8fd9\u4e9b\u95ee\u9898\u90fd\u53ef\u4ee5\u901a\u8fc7\u5c06\u590d\u6742\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u7b80\u5355\u7684\u5b50\u95ee\u9898\u6765\u6709\u6548\u89e3\u51b3\u3002<\/p>\n<p><strong>\u9012\u5f52\u7684\u6027\u80fd\u5982\u4f55\u4f18\u5316\uff1f<\/strong><br \/>\u9012\u5f52\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff0c\u5982\u6808\u6ea2\u51fa\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5c3e\u9012\u5f52\u4f18\u5316\uff08\u867d\u7136Python\u672c\u8eab\u4e0d\u652f\u6301\uff09\uff0c\u6216\u4f7f\u7528\u8fed\u4ee3\u65b9\u6cd5\u6765\u4ee3\u66ff\u9012\u5f52\u3002\u6b64\u5916\uff0c\u8bb0\u5fc6\u5316\uff08memoization\uff09\u662f\u4e00\u79cd\u6709\u6548\u7684\u4f18\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u5b58\u50a8\u5df2\u7ecf\u8ba1\u7b97\u8fc7\u7684\u7ed3\u679c\uff0c\u4ece\u800c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u9012\u5f52\u662f\u4e00\u79cd\u5728\u51fd\u6570\u4e2d\u8c03\u7528\u81ea\u8eab\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u5b83\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u53ef\u4ee5\u88ab\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u3001\u76f8\u4f3c\u95ee\u9898\u7684\u95ee\u9898\u3002\u9012\u5f52 [&hellip;]","protected":false},"author":3,"featured_media":1020112,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020105"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1020105"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020105\/revisions"}],"predecessor-version":[{"id":1020116,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1020105\/revisions\/1020116"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1020112"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1020105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1020105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1020105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}