{"id":256,"date":"2023-12-11T16:10:44","date_gmt":"2023-12-11T08:10:44","guid":{"rendered":"http:\/\/ericw.top\/?p=256"},"modified":"2023-12-11T16:10:44","modified_gmt":"2023-12-11T08:10:44","slug":"java-%e9%80%92%e5%bd%92","status":"publish","type":"post","link":"http:\/\/ericw.top\/?p=256","title":{"rendered":"JAVA \u9012\u5f52"},"content":{"rendered":"<h1 id=\"toc-0\">JAVA \u9012\u5f52<\/h1>\n<h2 id=\"toc-1\">\u4ec0\u4e48\u662f\u9012\u5f52\uff1f<\/h2>\n<p><strong>\u9012\u5f52<\/strong>\u662f\u4e00\u79cd\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\uff0c\u5176\u4e2d\u4e00\u4e2a\u51fd\u6570\u901a\u8fc7\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u66f4\u5c0f\u89c4\u6a21\u7684\u95ee\u9898\uff0c\u76f4\u5230\u8fbe\u5230\u57fa\u672c\u60c5\u51b5\u4e3a\u6b62\u3002<strong>\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u7ecf\u5178\u7684\u4f8b\u5b50\uff1a\u9636\u4e58\u3002<\/strong><\/p>\n<h3 id=\"toc-2\">\u9636\u4e58\u7684\u9012\u5f52\u5b9e\u73b0<\/h3>\n<p>\u9636\u4e58\u662f\u4e00\u4e2a\u81ea\u7136\u6570\u7684\u4e58\u79ef\uff0c\u4ece1\u5230\u8be5\u6570\u7684\u6240\u6709\u6b63\u6574\u6570\u7684\u4e58\u79ef\u3002\u7528\u6570\u5b66\u8868\u793a\u4e3a<code class=\"prettyprint\" >n! = n * (n-1) * (n-2) * ... * 1<\/code>\u3002\u5728Java\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u6765\u8ba1\u7b97\u9636\u4e58\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u9636\u4e58\u9012\u5f52\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"prettyprint\"  class=\"language-java\">public class Factorial {\n    public static int factorial(int n) {\n        if (n &lt;= 1) {\n            return 1; \/\/ \u57fa\u672c\u60c5\u51b5\uff1a0\u7684\u9636\u4e58\u4e3a1\n        } else {\n            return n * factorial(n - 1); \/\/ \u9012\u5f52\u8c03\u7528\n        }\n    }\n\n    public static void main(String[] args) {\n        int result = factorial(5);\n        System.out.println(&quot;5\u7684\u9636\u4e58\u662f\uff1a&quot; + result); \/\/ \u8f93\u51fa 5\u7684\u9636\u4e58\u662f\uff1a120\n    }\n}<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code class=\"prettyprint\" >factorial<\/code>\u51fd\u6570\u901a\u8fc7\u4e0d\u65ad\u8c03\u7528\u81ea\u8eab\uff0c\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230<code class=\"prettyprint\" >n<\/code>\u7b49\u4e8e1\u4e3a\u6b62\uff0c\u7136\u540e\u8fd4\u56de1\u3002\u7136\u540e\u9012\u5f52\u51fd\u6570\u5f00\u59cb\u5408\u5e76\u8fd9\u4e9b\u5b50\u95ee\u9898\u7684\u7ed3\u679c\uff0c\u76f4\u5230\u5f97\u5230\u6700\u7ec8\u7b54\u6848120\u3002\u8fd9\u5c31\u662f\u9012\u5f52\u7684\u57fa\u672c\u601d\u60f3\u3002<\/p>\n<h2 id=\"toc-3\">\u9012\u5f52\u7684\u57fa\u672c\u8981\u7d20<\/h2>\n<ol>\n<li><strong>\u57fa\u672c\u60c5\u51b5\uff08Base Case\uff09<\/strong><br \/>\n\u57fa\u672c\u60c5\u51b5\u662f\u9012\u5f52\u7b97\u6cd5\u4e2d\u7684\u505c\u6b62\u6761\u4ef6\u3002\u5728\u9636\u4e58\u7684\u4f8b\u5b50\u4e2d\uff0c\u57fa\u672c\u60c5\u51b5\u662f\u5f53n\u7b49\u4e8e1\u65f6\uff0c\u8fd4\u56de1\u3002\u57fa\u672c\u60c5\u51b5\u7684\u5b58\u5728\u662f\u9632\u6b62\u9012\u5f52\u65e0\u9650\u5faa\u73af\u7684\u5173\u952e\u3002<\/li>\n<li><strong>\u9012\u5f52\u8c03\u7528\uff08Recursive Call\uff09<\/strong><br \/>\n\u9012\u5f52\u8c03\u7528\u662f\u51fd\u6570\u5728\u81ea\u8eab\u5185\u90e8\u8c03\u7528\u81ea\u8eab\u7684\u8fc7\u7a0b\u3002\u5728\u9636\u4e58\u7684\u4f8b\u5b50\u4e2d\uff0c\u51fd\u6570\u5728return n * factorial(n - 1)\u5904\u8c03\u7528\u4e86\u81ea\u8eab\uff0c\u8fd9\u662f\u9012\u5f52\u7684\u6838\u5fc3\u3002<\/li>\n<li><strong>\u95ee\u9898\u89c4\u6a21\u7684\u51cf\u5c0f<\/strong><br \/>\n\u9012\u5f52\u7b97\u6cd5\u5fc5\u987b\u80fd\u591f\u5c06\u539f\u59cb\u95ee\u9898\u5206\u89e3\u4e3a\u89c4\u6a21\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u8fbe\u5230\u57fa\u672c\u60c5\u51b5\u3002\u5728\u9636\u4e58\u7684\u4f8b\u5b50\u4e2d\uff0c\u95ee\u9898\u89c4\u6a21\u51cf\u5c0f\u662f\u901a\u8fc7\u6bcf\u6b21\u5c06n\u51cf\u5c111\u6765\u5b9e\u73b0\u7684\uff0c\u76f4\u5230n\u7b49\u4e8e1\u4e3a\u6b62\u3002<\/li>\n<\/ol>\n<h2 id=\"toc-4\">\u9012\u5f52\u7684\u6267\u884c\u8fc7\u7a0b<\/h2>\n<pre><code class=\"prettyprint\"  class=\"language-java\">public class RecursionExample {\n    public static void recursiveFunction(int n) {\n        if (n &lt;= 0) {\n            return;\n        }\n        System.out.println(&quot;Before recursive call: &quot; + n);\n        recursiveFunction(n - 1);\n        System.out.println(&quot;After recursive call: &quot; + n);\n    }\n\n    public static void main(String[] args) {\n        recursiveFunction(3);\n    }\n}<\/code><\/pre>\n<pre><code class=\"prettyprint\"  class=\"language-java\">\/\/\u4e0a\u9762\u4ee3\u7801\u7684\u8f93\u51fa\uff1a\nBefore recursive call: 3\nBefore recursive call: 2\nBefore recursive call: 1\nAfter recursive call: 1\nAfter recursive call: 2\nAfter recursive call: 3<\/code><\/pre>\n<h2 id=\"toc-5\">\u9012\u5f52\u7684\u5e94\u7528<\/h2>\n<p>\u9012\u5f52\u4e0d\u4ec5\u4ec5\u7528\u4e8e\u9636\u4e58\uff0c\u4ed6\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u548c\u7f16\u7a0b\u4e2d\u6709\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u3002<\/p>\n<ol>\n<li><strong>\u6590\u6ce2\u90a3\u5951\u6570\u5217<\/strong><br \/>\n\u6590\u6ce2\u90a3\u5951\u6570\u5217\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u9012\u5f52\u95ee\u9898\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6570\u5b57\u662f\u524d\u4e24\u4e2a\u6570\u5b57\u7684\u548c\u3002\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u9012\u5f52\u5b9a\u4e49\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre><code class=\"prettyprint\"  class=\"language-java\">fib(0) = 0\nfib(1) = 1\nfib(n) = fib(n-1) + fib(n-2)<\/code><\/pre>\n<ol start=\"2\">\n<li><strong>\u6587\u4ef6\u7cfb\u7edf\u904d\u5386<\/strong><br \/>\n\u5728\u5904\u7406\u6587\u4ef6\u7cfb\u7edf\u65f6\uff0c\u9012\u5f52\u53ef\u7528\u4e8e\u904d\u5386\u6587\u4ef6\u5939\u548c\u5b50\u6587\u4ef6\u5939\uff0c\u4ee5\u67e5\u627e\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u6216\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u6570\u636e\u7ed3\u6784\u64cd\u4f5c<\/strong><br \/>\n\u9012\u5f52\u5728\u5904\u7406\u6811\u3001\u56fe\u7b49\u6570\u636e\u7ed3\u6784\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u4e8c\u53c9\u6811\u7684\u904d\u5386\u548c\u641c\u7d22\u901a\u5e38\u4f7f\u7528\u9012\u5f52\u7b97\u6cd5\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u6392\u5217\u548c\u7ec4\u5408<\/strong><br \/>\n\u9012\u5f52\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u6392\u5217\u548c\u7ec4\u5408\uff0c\u5982\u6392\u5217\u95ee\u9898\uff08n\u4e2a\u5143\u7d20\u7684\u5168\u6392\u5217\uff09\u548c\u7ec4\u5408\u95ee\u9898\uff08\u4ecen\u4e2a\u5143\u7d20\u4e2d\u9009\u62e9k\u4e2a\u5143\u7d20\u7684\u7ec4\u5408\uff09\u3002<\/li>\n<\/ol>\n<h2 id=\"toc-6\">\u9012\u5f52\u7684\u6027\u80fd\u548c\u6ce8\u610f\u4e8b\u9879<\/h2>\n<p>\u5c3d\u7ba1<strong>\u9012\u5f52\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177<\/strong>\uff0c\u4f46\u5b83\u4e0d\u603b\u662f\u6700\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u9012\u5f52\u51fd\u6570\u7684\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u5806\u6808\u6df1\u5ea6\u7684\u9650\u5236\uff0c\u800c\u4e14\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5806\u6808\u6ea2\u51fa\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u8fed\u4ee3\u6216\u52a8\u6001\u89c4\u5212\u7b49\u5176\u4ed6\u65b9\u6cd5\u6765\u4f18\u5316\u9012\u5f52\u7b97\u6cd5\u3002<\/p>\n<p>\u6b64\u5916\uff0c<strong>\u9012\u5f52\u51fd\u6570\u7684\u8c03\u7528\u6b21\u6570\u53ef\u80fd\u4f1a\u5f88\u591a\uff0c\u56e0\u6b64\u9700\u8981\u5c0f\u5fc3\uff0c\u4ee5\u786e\u4fdd\u5b83\u4e0d\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002<\/strong>\u5728\u4e00\u4e9b\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c<strong>\u5c3e\u9012\u5f52\u4f18\u5316\u53ef\u4ee5\u5e2e\u52a9\u51cf\u5c11\u9012\u5f52\u8c03\u7528\u7684\u5f00\u9500<\/strong>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JAVA \u9012\u5f52 \u4ec0\u4e48\u662f\u9012\u5f52\uff1f \u9012\u5f52\u662f\u4e00\u79cd\u89e3\u51b3\u95ee\u9898\u7684\u65b9\u6cd5\uff0c\u5176\u4e2d\u4e00\u4e2a\u51fd\u6570\u901a\u8fc7\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u66f4\u5c0f\u89c4\u6a21\u7684\u95ee\u9898\uff0c\u76f4\u5230\u8fbe\u5230 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":257,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,5],"tags":[],"_links":{"self":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/256"}],"collection":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=256"}],"version-history":[{"count":1,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/256\/revisions"}],"predecessor-version":[{"id":258,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/256\/revisions\/258"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/media\/257"}],"wp:attachment":[{"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=256"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}