{"id":20,"date":"2019-03-10T14:26:19","date_gmt":"2019-03-10T06:26:19","guid":{"rendered":"http:\/\/www.guanhaobo.cn\/?p=20"},"modified":"2019-03-10T14:26:19","modified_gmt":"2019-03-10T06:26:19","slug":"hdu-2041-%e8%b6%85%e7%ba%a7%e6%a5%bc%e6%a2%af","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=20","title":{"rendered":"HDU 2041 \u2014 \u8d85\u7ea7\u697c\u68af"},"content":{"rendered":"<h4>\u9898\u76ee\u5927\u610f<\/h4>\n<p>\u6709\u4e00\u697c\u68af\u5171M\u7ea7\uff0c\u521a\u5f00\u59cb\u65f6\u4f60\u5728\u7b2c\u4e00\u7ea7\uff0c\u6bcf\u6b21\u53ea\u80fd\u8de8\u4e0a\u4e00\u7ea7\u6216\u4e8c\u7ea7\u3002\u8981\u8d70\u4e0a\u7b2cM\u7ea7\uff0c\u5171\u6709\u591a\u5c11\u79cd\u8d70\u6cd5\uff1f<\/p>\n<p><!--more--><\/p>\n<h4>\u9898\u76ee\u94fe\u63a5<\/h4>\n<p><a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2041\" title=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2041\">http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2041<\/a><\/p>\n<h4>\u8f93\u5165<\/h4>\n<p>\u8f93\u5165\u6570\u636e\u9996\u5148\u5305\u542b\u4e00\u4e2a\u6574\u6570N\uff0c\u8868\u793a\u6d4b\u8bd5\u5b9e\u4f8b\u7684\u4e2a\u6570\uff0c\u7136\u540e\u662fN\u884c\u6570\u636e\uff0c\u6bcf\u884c\u5305\u542b\u4e00\u4e2a\u6574\u6570M\uff081&lt;=M&lt;=40\uff09,\u8868\u793a\u697c\u68af\u7684\u7ea7\u6570\u3002<\/p>\n<h4>\u8f93\u51fa<\/h4>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u6d4b\u8bd5\u5b9e\u4f8b\uff0c\u8bf7\u8f93\u51fa\u4e0d\u540c\u8d70\u6cd5\u7684\u6570\u91cf\u3002<\/p>\n<h4>\u6837\u4f8b\u8f93\u5165<\/h4>\n<pre><code class=\"line-numbers\">2\n2\n3\n<\/code><\/pre>\n<h4>\u6837\u4f8b\u8f93\u51fa<\/h4>\n<pre><code class=\"line-numbers\">1\n2\n<\/code><\/pre>\n<h4>\u95ee\u9898\u5206\u6790<\/h4>\n<p>\u6211\u4eec\u8981\u6c42\u8d70\u4e0a\u7b2c n \u7ea7\u53f0\u9636\u7684\u8d70\u6cd5\uff08\u4e3a\u4ec0\u4e48\u9898\u76ee\u4e2d\u662fM\uff0c\u8fd9\u91cc\u5374\u7528 n \uff0c\u56e0\u4e3a\u6211\u559c\u6b22\u7528 n \uff09\uff0c\u4e0d\u59a8\u5148\u770b\u4e00\u4e0b\u7b2c n \u7ea7\u53f0\u9636\u7684\u524d\u4e00\u6b65\u662f\u4ec0\u4e48\uff0c\u5f88\u5bb9\u6613\u53d1\u73b0\u524d\u4e00\u6b65\u5fc5\u5b9a\u662f\u7b2c n-1 \u7ea7\u53f0\u9636\u6216\u7b2c n-2 \u7ea7\u53f0\u9636\uff08\u6bcf\u6b21\u53ea\u80fd\u8d70\u4e00\u7ea7\u6216\u4e24\u7ea7\uff09\u3002\u6240\u4ee5\u8d70\u4e0a\u7b2c n \u7ea7\u53f0\u9636\u7684\u8d70\u6cd5\u5c31\u7b49\u4e8e\u8d70\u4e0a\u7b2c n-1 \u7ea7\u548c\u7b2c n-2 \u7ea7\u7684\u8d70\u6cd5\u7684\u548c\u3002\u7528\u51fd\u6570 f(n) \u8868\u793a\u8d70\u4e0a\u7b2c n \u7ea7\u53f0\u9636\u7684\u8d70\u6cd5\uff0c\u5219f(n) = f(n-1) + f(n-2).<\/p>\n<p>\u76f8\u4fe1\u5927\u90e8\u5206\u8bfb\u8005\u5df2\u7ecf\u77e5\u9053\u600e\u4e48\u505a\u4e86\uff0c\u771f\u76f8\u5c31\u662f\u2014\u2014\u9012\u5f52\u6216\u9012\u63a8\uff01\u9012\u5f52\u548c\u9012\u63a8\u90fd\u80fd\u591fAC\u8fd9\u9053\u9898\uff0c\u4f46\u9012\u5f52\u8017\u65f6\u8f83\u957f\u3002<\/p>\n<h4>AC\u4ee3\u7801\u2014\u2014\u9012\u5f52<\/h4>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;iostream&gt;\nusing namespace std;\nint f(int n)\n{\n    switch (n)\n    {\n    case 1:\n        return 0;\n    case 2:\n        return 1;\n    case 3:\n        return 2;\n    }\n    return f(n - 1) + f(n - 2);\n}\nint main()\n{\n    int n, t;\n    cin &gt;&gt; t;\n    while (t--)\n        while (cin &gt;&gt; n)\n            cout &lt;&lt; f(n) &lt;&lt; endl;\n}\n<\/code><\/pre>\n<h4>AC\u4ee3\u7801\u2014\u2014\u9012\u63a8<\/h4>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;iostream&gt;\nusing namespace std;\nint main()\n{\n    int n, t, i, f[40];\n    f[1] = 0, f[2] = 1, f[3] = 2;\n    for (i = 4; i &lt;= 40; i++)\n        f[i] = f[i - 1] + f[i - 2];\n    cin &gt;&gt; t;\n    while (t--)\n        while (cin &gt;&gt; n)\n            cout &lt;&lt; f[n] &lt;&lt; endl;\n}\n<\/code><\/pre>\n<h4>\u6ce8\u610f\u4e8b\u9879<\/h4>\n<p>1\u3001\u9012\u5f52\u8017\u65f6\u8f83\u957f\uff0c\u4f46\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u9012\u5f52\u6bd4\u8f83\u5bb9\u6613\u601d\u8003\u3002<br \/>\n2\u3001\u7c7b\u4f3c\u7684\u9898\u76ee\u5982\u679c\u6570\u636e\u6bd4\u8f83\u5927\uff0cint \u662f\u5b58\u4e0d\u4e0b\u7684\u3002\u4f46\u672c\u9898\u697c\u68af\u7ea7\u6570M\u6700\u5927\u662f40\uff0c\u5728\u672c\u5730\u5224\u65ad\u4e00\u4e0b\u4f1a\u53d1\u73b0\u7528 int \u5c31\u8db3\u591f\u4e86\u3002<br \/>\n3\u3001\u56e0\u4e3a\u6b64\u9898\u6709\u591a\u7ec4\u6570\u636e\u8f93\u5165\uff0c\u6240\u4ee5\u7ed9\u51fa\u7684\u9012\u63a8\u4ee3\u7801\u4e00\u6b21\u6027\u5c06\u6240\u6709\u7684\u7b54\u6848\u90fd\u8ba1\u7b97\u51fa\u6765\uff0c\u8282\u7701\u4e86\u4e0d\u5c11\u65f6\u95f4\u3002\u5982\u679c\u53ea\u6709\u4e00\u7ec4\u6570\u636e\u5219\u4e0d\u5fc5\u5168\u90e8\u8ba1\u7b97\u51fa\u6765\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f \u6709\u4e00\u697c\u68af\u5171M\u7ea7\uff0c\u521a\u5f00\u59cb\u65f6\u4f60\u5728\u7b2c\u4e00\u7ea7\uff0c\u6bcf\u6b21\u53ea\u80fd\u8de8\u4e0a\u4e00\u7ea7\u6216\u4e8c\u7ea7\u3002\u8981\u8d70\u4e0a\u7b2cM\u7ea7\uff0c\u5171\u6709\u591a\u5c11\u79cd\u8d70\u6cd5\uff1f<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[14,64],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-algo","tag-hdu","tag-64"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20"}],"version-history":[{"count":0,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/20\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}