{"id":1025705,"date":"2024-12-31T10:36:18","date_gmt":"2024-12-31T02:36:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1025705.html"},"modified":"2024-12-31T10:36:22","modified_gmt":"2024-12-31T02:36:22","slug":"python%e5%a6%82%e4%bd%95%e5%88%b6%e4%bd%9c%e7%ab%8b%e6%96%b9%e6%a0%b9%e7%9a%84%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1025705.html","title":{"rendered":"python\u5982\u4f55\u5236\u4f5c\u7acb\u65b9\u6839\u7684\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/0b4aa459-a32a-471d-83d2-28456745c708.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5236\u4f5c\u7acb\u65b9\u6839\u7684\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5236\u4f5c\u7acb\u65b9\u6839\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u5e42\u8fd0\u7b97\u7b26\u3001math\u5e93\u6216\u81ea\u5b9a\u4e49\u65b9\u6cd5<\/strong>\u3002\u5176\u4e2d\u4f7f\u7528\u5185\u7f6e\u7684\u5e42\u8fd0\u7b97\u7b26\u662f\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u548c\u53ef\u8bfb\u6027\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e09\u79cd\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26 <code><\/code> \u6765\u8ba1\u7b97\u7acb\u65b9\u6839\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u7acb\u65b9\u6839\u76f8\u5f53\u4e8e\u5c06\u4e00\u4e2a\u6570\u63d0\u5347\u5230 <code>1\/3<\/code> \u6b21\u65b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def cube_root(n):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u7acb\u65b9\u6839<\/p>\n<p>    :param n: \u9700\u8981\u8ba1\u7b97\u7acb\u65b9\u6839\u7684\u6570<\/p>\n<p>    :return: \u8be5\u6570\u7684\u7acb\u65b9\u6839<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return n  (1 \/ 3)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(cube_root(27))  # \u8f93\u51fa\uff1a3.0<\/p>\n<p>print(cube_root(-8))  # \u8f93\u51fa\uff1a-2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528math\u5e93<\/h3>\n<\/p>\n<p><p>Python\u7684 <code>math<\/code> \u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\uff0c\u4f46\u5e76\u6ca1\u6709\u76f4\u63a5\u7684\u7acb\u65b9\u6839\u51fd\u6570\u3002\u4e0d\u8fc7\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>math.pow<\/code> \u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def cube_root(n):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u7acb\u65b9\u6839<\/p>\n<p>    :param n: \u9700\u8981\u8ba1\u7b97\u7acb\u65b9\u6839\u7684\u6570<\/p>\n<p>    :return: \u8be5\u6570\u7684\u7acb\u65b9\u6839<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return math.pow(n, 1 \/ 3)<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(cube_root(27))  # \u8f93\u51fa\uff1a3.0<\/p>\n<p>print(cube_root(-8))  # \u8f93\u51fa\uff1a-2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u81ea\u5b9a\u4e49\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u7279\u6b8a\u9700\u6c42\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u81ea\u5df1\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u7acb\u65b9\u6839\uff0c\u4f8b\u5982\u4f7f\u7528\u725b\u987f\u8fed\u4ee3\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u53ef\u4ee5\u63a7\u5236\u7cbe\u5ea6\u548c\u8fed\u4ee3\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def cube_root(n, epsilon=1e-7):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u7acb\u65b9\u6839\uff0c\u4f7f\u7528\u725b\u987f\u8fed\u4ee3\u6cd5<\/p>\n<p>    :param n: \u9700\u8981\u8ba1\u7b97\u7acb\u65b9\u6839\u7684\u6570<\/p>\n<p>    :param epsilon: \u7cbe\u5ea6<\/p>\n<p>    :return: \u8be5\u6570\u7684\u7acb\u65b9\u6839<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    guess = n \/ 3.0<\/p>\n<p>    while abs(guess3 - n) &gt;= epsilon:<\/p>\n<p>        guess = guess - ((guess&lt;strong&gt;3 - n) \/ (3 * guess&lt;\/strong&gt;2))<\/p>\n<p>    return guess<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>print(cube_root(27))  # \u8f93\u51fa\uff1a3.0<\/p>\n<p>print(cube_root(-8))  # \u8f93\u51fa\uff1a-2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8be6\u7ec6\u89e3\u6790<\/h3>\n<\/p>\n<p><h4>\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u5176\u672c\u8d28\u662f\u5229\u7528\u4e86\u6570\u5b66\u4e2d\u7684\u5e42\u8fd0\u7b97\u6027\u8d28\u3002\u5c06\u4e00\u4e2a\u6570 <code>n<\/code> \u63d0\u5347\u5230 <code>1\/3<\/code> \u6b21\u65b9\uff0c\u5373 <code>n  (1\/3)<\/code>\uff0c\u53ef\u4ee5\u5f97\u5230\u5176\u7acb\u65b9\u6839\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u5408\u5927\u591a\u6570\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528math\u5e93<\/h4>\n<\/p>\n<p><p><code>math.pow<\/code> \u662f Python \u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e42\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528 <code>math.pow(n, 1\/3)<\/code> \u6765\u8ba1\u7b97\u7acb\u65b9\u6839\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0e\u5e42\u8fd0\u7b97\u7b26\u7c7b\u4f3c\uff0c\u4f46 <code>math.pow<\/code> \u662f\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\uff0c\u53ef\u80fd\u5728\u67d0\u4e9b\u5177\u4f53\u5b9e\u73b0\u4e0a\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h4>\u81ea\u5b9a\u4e49\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u725b\u987f\u8fed\u4ee3\u6cd5\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u6c42\u89e3\u5404\u79cd\u65b9\u7a0b\u7684\u6839\u3002\u5bf9\u4e8e\u7acb\u65b9\u6839\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u725b\u987f\u8fed\u4ee3\u6cd5\u6765\u903c\u8fd1\u7ed3\u679c\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521d\u59cb\u5316\u4e00\u4e2a\u731c\u6d4b\u503c <code>guess<\/code>\u3002<\/li>\n<li>\u8ba1\u7b97 <code>guess<\/code> \u7684\u7acb\u65b9\u4e0e <code>n<\/code> \u7684\u5dee\u503c\u3002<\/li>\n<li>\u6839\u636e\u725b\u987f\u8fed\u4ee3\u516c\u5f0f\u66f4\u65b0 <code>guess<\/code>\u3002<\/li>\n<li>\u91cd\u590d\u6b65\u9aa42\u548c3\uff0c\u76f4\u5230\u5dee\u503c\u5c0f\u4e8e\u6307\u5b9a\u7684\u7cbe\u5ea6 <code>epsilon<\/code>\u3002<\/li>\n<\/ol>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u53ef\u4ee5\u63a7\u5236\u8fed\u4ee3\u6b21\u6570\u548c\u7cbe\u5ea6\uff0c\u9002\u7528\u4e8e\u5bf9\u8ba1\u7b97\u7ed3\u679c\u6709\u8f83\u9ad8\u8981\u6c42\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8ba1\u7b97\u7acb\u65b9\u6839<\/strong>\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5e42\u8fd0\u7b97\u7b26 <code><\/code>\uff0c\u5176\u6b21\u662f\u4f7f\u7528 <code>math<\/code> \u5e93\u4e2d\u7684 <code>math.pow<\/code> \u51fd\u6570\u3002\u5bf9\u4e8e\u4e00\u4e9b\u7279\u6b8a\u9700\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528\u725b\u987f\u8fed\u4ee3\u6cd5\u6765\u81ea\u5b9a\u4e49\u7acb\u65b9\u6839\u51fd\u6570\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u76f8\u4fe1\u5927\u5bb6\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u7acb\u65b9\u6839\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u7acb\u65b9\u6839\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>&lt;strong&gt;<\/code>\u8fd0\u7b97\u7b26\u6765\u8ba1\u7b97\u7acb\u65b9\u6839\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>x &lt;\/strong&gt; (1\/3)<\/code>\u53ef\u4ee5\u83b7\u5f97\u6570\u5b57x\u7684\u7acb\u65b9\u6839\u3002\u6b64\u5916\uff0cPython\u7684<code>math<\/code>\u6a21\u5757\u4e5f\u63d0\u4f9b\u4e86<code>math.pow()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u7acb\u65b9\u6839\uff0c\u8bed\u6cd5\u4e3a<code>math.pow(x, 1\/3)<\/code>\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u79cd\u6570\u636e\u7c7b\u578b\u6765\u8ba1\u7b97\u7acb\u65b9\u6839\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u6570\u548c\u6d6e\u70b9\u6570\u3002\u65e0\u8bba\u662f\u8ba1\u7b97\u6574\u6570\u7684\u7acb\u65b9\u6839\u8fd8\u662f\u6d6e\u70b9\u6570\u7684\u7acb\u65b9\u6839\uff0cPython\u90fd\u80fd\u6b63\u786e\u5904\u7406\u3002\u786e\u4fdd\u8f93\u5165\u7684\u6570\u5b57\u662f\u6709\u6548\u7684\uff0c\u907f\u514d\u4f20\u5165\u8d1f\u503c\uff0c\u56e0\u4e3a\u7acb\u65b9\u6839\u7684\u8ba1\u7b97\u5728\u6570\u5b66\u4e0a\u662f\u5b9a\u4e49\u826f\u597d\u7684\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d1f\u6570\u7684\u7acb\u65b9\u6839\uff1f<\/strong><br \/>\u8ba1\u7b97\u8d1f\u6570\u7684\u7acb\u65b9\u6839\u65f6\uff0cPython\u80fd\u591f\u6b63\u786e\u8fd4\u56de\u7ed3\u679c\u3002\u4f8b\u5982\uff0c<code>(-8) ** (1\/3)<\/code>\u5c06\u8fd4\u56de-2\uff0c\u8fd9\u662f\u56e0\u4e3a-2\u7684\u7acb\u65b9\u662f-8\u3002\u786e\u4fdd\u4f7f\u7528\u5408\u9002\u7684\u6570\u5b66\u8fd0\u7b97\u7b26\u548c\u62ec\u53f7\u4ee5\u83b7\u5f97\u6b63\u786e\u7684\u7ed3\u679c\u3002\u5728Python\u4e2d\uff0c\u8d1f\u6570\u7684\u7acb\u65b9\u6839\u8ba1\u7b97\u4e0d\u4f1a\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5236\u4f5c\u7acb\u65b9\u6839\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u5e42\u8fd0\u7b97\u7b26\u3001math\u5e93\u6216\u81ea\u5b9a\u4e49\u65b9\u6cd5\u3002\u5176\u4e2d\u4f7f\u7528\u5185\u7f6e\u7684\u5e42\u8fd0\u7b97\u7b26\u662f [&hellip;]","protected":false},"author":3,"featured_media":1025719,"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\/1025705"}],"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=1025705"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025705\/revisions"}],"predecessor-version":[{"id":1025721,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1025705\/revisions\/1025721"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1025719"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1025705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1025705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1025705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}