{"id":39276,"date":"2025-05-23T09:48:54","date_gmt":"2025-05-23T01:48:54","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/39276.html"},"modified":"2025-05-23T09:48:54","modified_gmt":"2025-05-23T01:48:54","slug":"%e3%80%90%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%e3%80%91%e6%a0%88","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/39276.html","title":{"rendered":"\u3010\u6570\u636e\u7ed3\u6784\u3011\u6808"},"content":{"rendered":"<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"1.1%20%E6%A0%88%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5-toc\" style=\"margin-left:0px\">1.1 \u6808\u7684\u57fa\u672c\u6982\u5ff5<\/p>\n<p id=\"1.1.1%20%E6%A0%88%E7%9A%84%E6%8A%BD%E8%B1%A1%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B-toc\" style=\"margin-left:40px\">1.1.1 \u6808\u7684\u62bd\u8c61\u6570\u636e\u7c7b\u578b<\/p>\n<p id=\"1.1.2%20%E9%A1%BA%E5%BA%8F%E6%A0%88-toc\" style=\"margin-left:40px\">1.1.2 \u987a\u5e8f\u6808<\/p>\n<p id=\"1.1.3%20%E9%93%BE%E5%BC%8F%E6%A0%88-toc\" style=\"margin-left:40px\">1.1.3 \u94fe\u5f0f\u6808<\/p>\n<p id=\"1.2%C2%A0%E6%A0%88%E4%B8%8E%E9%80%92%E5%BD%92-toc\" style=\"margin-left:0px\">1.2\u00a0\u6808\u4e0e\u9012\u5f52<\/p>\n<p id=\"1.2.1%20%E9%80%92%E5%BD%92%E7%9A%84%E5%AE%9A%E4%B9%89-toc\" style=\"margin-left:40px\">1.2.1 \u9012\u5f52\u7684\u5b9a\u4e49<\/p>\n<p id=\"1.2.2%20%E9%80%92%E5%BD%92%E5%87%BD%E6%95%B0%E7%9A%84%E5%AE%9E%E7%8E%B0-toc\" style=\"margin-left:40px\">1.2.2 \u9012\u5f52\u51fd\u6570\u7684\u5b9e\u73b0<\/p>\n<p id=\"1.2.3%20%E9%80%92%E5%BD%92%E7%AE%97%E6%B3%95%E5%88%B0%E9%9D%9E%E9%80%92%E5%BD%92%E7%9A%84%E8%BD%AC%E6%8D%A2-toc\" style=\"margin-left:40px\">1.2.3 \u9012\u5f52\u7b97\u6cd5\u5230\u975e\u9012\u5f52\u7684\u8f6c\u6362<\/p>\n<p id=\"1.3%C2%A0%E6%A0%88%E7%9A%84%E6%B7%B1%E5%85%A5%E8%AE%A8%E8%AE%BA-toc\" style=\"margin-left:0px\">1.3\u00a0\u6808\u7684\u6df1\u5165\u8ba8\u8bba<\/p>\n<p id=\"1.3.1%20%E9%A1%BA%E5%BA%8F%E6%A0%88%E4%B8%8E%E9%93%BE%E5%BC%8F%E6%A0%88%E7%9A%84%E6%AF%94%E8%BE%83-toc\" style=\"margin-left:40px\">1.3.1 \u987a\u5e8f\u6808\u4e0e\u94fe\u5f0f\u6808\u7684\u6bd4\u8f83<\/p>\n<p id=\"1.3.2%20%E9%99%90%E5%88%B6%E5%AD%98%E5%8F%96%E7%82%B9%E7%9A%84%E8%A1%A8-toc\" style=\"margin-left:40px\">1.3.2 \u9650\u5236\u5b58\u53d6\u70b9\u7684\u8868<\/p>\n<p id=\"%E6%9C%AC%E7%AB%A0%E5%B0%8F%E7%BB%93-toc\" style=\"margin-left:0px\">\u672c\u7ae0\u5c0f\u7ed3<\/p>\n<hr \/>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6808(stack)\u548c\u961f\u5217(queue)\u4f5c\u4e3a\u4e24\u79cd\u91cd\u8981\u7684\u7ebf\u6027\u7ed3\u6784,\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u5177\u6709\u975e\u5e38\u5e7f\u6cdb\u7684\u5e94\u7528&#xff0c;\u4ece\u7b80\u5355\u7684\u8868\u8fbe\u5f0f\u8ba1\u7b97\u5230\u7f16\u8bd1\u5668\u5bf9\u7a0b\u5e8f\u8bed\u6cd5\u7684\u68c0\u67e5,\u518d\u5230\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u5404\u79cd\u8bbe\u5907\u7684\u7ba1\u7406\u7b49\u90fd\u4f1a\u6d89\u53ca\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4ece\u903b\u8f91\u7ed3\u6784\u6765\u8bf4,\u6808\u548c\u961f\u5217\u90fd\u662f\u5178\u578b\u7684\u7ebf\u6027\u7ed3\u6784\u3002\u4e0e\u7ebf\u6027\u8868\u4e0d\u540c\u7684\u662f,\u6808\u548c\u961f\u5217\u4e0a\u7684\u64cd\u4f5c\u6bd4\u8f83\u7279\u6b8a,\u53d7\u5230\u4e00\u5b9a\u7684\u9650\u5236,\u4ec5\u5141\u8bb8\u5728\u7ebf\u6027\u8868\u7684\u4e00\u7aef\u6216\u4e24\u7aef\u8fdb\u884c\u3002\u56e0\u6b64,\u6808\u548c\u961f\u5217\u5e38\u88ab\u79f0\u4e3a\u64cd\u4f5c\u53d7\u9650\u7684\u7ebf\u6027\u8868,\u6216\u8005\u9650\u5236\u5b58\u53d6\u70b9\u7684\u7ebf\u6027\u8868\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6808\u548c\u961f\u5217\u5728\u65e5\u5e38\u751f\u6d3b\u4e2d\u5747\u6709\u8bb8\u591a\u5b9e\u4f8b,\u4f8b\u5982\u53e0\u653e\u5728\u4e00\u8d77\u7684\u4e00\u645e\u76d8\u5b50\u53ef\u770b\u6210\u6808\u7684\u4e00\u4e2a\u5b9e\u4f8b,\u800c\u5728\u94f6\u884c\u6392\u961f\u53d6\u6b3e\u7684\u4e00\u884c\u4eba\u5219\u6784\u6210\u4e00\u4e2a\u5178\u578b\u7684\u961f\u5217\u3002\u4e00\u822c\u800c\u8a00,\u6808\u7684\u7279\u70b9\u662f\u540e\u8fdb\u5148\u51fa,\u5e38\u7528\u6765\u5904\u7406\u5177\u6709\u9012\u5f52\u7ed3\u6784\u7684\u6570\u636e;\u800c\u961f\u5217\u7684\u7279\u70b9\u5219\u662f\u5148\u8fdb\u5148\u51fa,\u5728\u5b9e\u9645\u4e2d\u4f53\u73b0\u51fa\u516c\u5e73\u7684\u539f\u5219,\u53ef\u4ee5\u7528\u6765\u6682\u65f6\u5b58\u653e\u9700\u8981\u6309\u7167\u4e00\u5b9a\u6b21\u5e8f\u4f9d\u6b21\u5904\u7406\u4f46\u5c1a\u672a\u5904\u7406\u7684\u5143\u7d20\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u672c\u7ae5\u5c06\u5bf9\u6808\u8fd9\u79cd\u7ed3\u6784\u53ca\u5176\u5178\u578b\u5e94\u7528\u8fdb\u884c\u4ecb\u7ecd\u3002<\/p>\n<h2 id=\"1.1%20%E6%A0%88%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5\">1.1 \u6808\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6808\u662f\u4e00\u79cd\u9650\u5b9a\u4ec5\u5728\u4e00\u7aef\u8fdb\u884c\u63d2\u5165\u548c\u5220\u9664\u7684\u7ebf\u6027\u8868,\u65e0\u8bba\u662f\u5f80\u6808\u4e2d\u63d2\u4eba\u5143\u7d20\u8fd8\u662f\u5220\u9664\u6808\u4e2d\u7684\u5143\u7d20,\u6216\u8005\u8bfb\u53d6\u6808\u4e2d\u7684\u5143\u7d20,\u90fd\u53ea\u80fd\u56fa\u5b9a\u5728\u7ebf\u6027\u8868\u7684\u4e00\u7aef\u8fdb\u884c\u3002\u901a\u5e38,\u6808\u7684\u8fd9\u4e00\u7aef\u88ab\u79f0\u4e3a\u6808\u9876(top),\u4e0e\u6b64\u76f8\u5bf9,\u6808\u7684\u53e6\u4e00\u7aef\u53eb\u505a\u6808\u5e95(bottom)\u3002\u7531\u6b64\u53ef\u77e5,\u6700\u540e\u63d2\u4eba\u6808\u4e2d\u7684\u5143\u7d20\u662f\u6700\u5148\u88ab\u5220\u9664\u8fdb\u6808\u6216\u8bfb\u53d6\u7684\u5143\u7d20,\u800c\u6700\u5148\u538b\u4eba\u7684\u5143\u7d20\u5219\u88ab\u653e\u5728\u6808\u7684\u5e95\u90e8,\u8981\u5230\u6700\u540e\u624d\u80fd\u53d6\u51fa\u3002\u6362\u8a00\u4e4b,\u6808\u7684\u4fee\u6539\u662f\u6309\u540e\u8fdb\u5148\u51fa\u7684\u539f\u5219\u8fdb\u884c\u3002\u56e0\u6b64&#xff0c;\u901a\u5e38\u6808\u88ab\u79f0\u4e3a\u540e\u8fdb\u5148\u51fa(lastinfirstout)\u8868,\u7b80\u79f0LIFO\u8868,\u5982\u56fe3.1\u6240\u793a\u3002\u4f8b\u5982,\u7897\u6a71\u91cc\u7684\u4e00\u53e0\u76d8\u5b50\u3001\u5927\u578b\u706b\u8f66\u7ad9\u7528\u4e8e\u8c03\u6574\u8c03\u5ea6\u706b\u8f66\u5934\u65b9\u5411\u7684\u8c03\u5ea6\u6808\u7ed3\u6784\u7b49\u90fd\u53ef\u89c6\u4e3a\u6808\u7684\u6a21\u578b\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"217\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014850-682fd40231b1a.png\" width=\"227\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5c3d\u7ba1\u64cd\u4f5c\u53d7\u9650\u964d\u4f4e\u4e86\u6808\u7684\u7075\u6d3b\u6027,\u4f46\u4e5f\u6b63\u56e0\u4e3a\u5982\u6b64\u800c\u4f7f\u5f97\u6808\u66f4\u6709\u6548\u4e14\u66f4\u5bb9\u6613\u5b9e\u73b0\u3002\u6808\u7684\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb,\u5e76\u56e0\u6b64\u5f62\u6210\u4e86\u6808\u7684\u4e00\u4e9b\u7279\u6b8a\u672f\u8bed\u3002\u4e60\u60ef\u4e0a\u79f0\u5f80\u6808\u4e2d\u63d2\u4eba\u5143\u7d20\u4e3apush\u64cd\u4f5c,\u7b80\u79f0\u4e3a\u538b\u6808\u6216\u4eba\u6808;\u5220\u9664\u6808\u9876\u5143\u7d20\u88ab\u79f0\u4e3apop\u64cd\u4f5c,\u7b80\u79f0\u4e3a\u51fa\u6808\u6216\u5f39\u51fa\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5982\u540c\u7ebf\u6027\u8868\u53ef\u4ee5\u4e3a\u7a7a\u8868\u4e00\u6837,\u6ca1\u6709\u5143\u7d20\u7684\u6808\u79f0\u4e3a\u7a7a\u6808\u3002\u4f8b\u5982,\u521a\u5efa\u7acb\u7684\u6808\u4e00\u822c\u662f\u7a7a\u6808,\u968f\u7740\u6808\u4e2d\u6240\u6709\u5143\u7d20\u7684\u5220\u9664,\u6808\u4e5f\u4f1a\u53d8\u6210\u7a7a\u6808\u3002<\/p>\n<h3 id=\"1.1.1%20%E6%A0%88%E7%9A%84%E6%8A%BD%E8%B1%A1%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B\">1.1.1 \u6808\u7684\u62bd\u8c61\u6570\u636e\u7c7b\u578b<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u57fa\u4e8e\u6808\u7684\u7279\u6027,\u5b9a\u4e49\u5728\u6808\u7684\u62bd\u8c61\u6570\u636e\u7c7b\u578b\u4e2d\u7684\u8fd0\u7b97\u5305\u62ec\u8fdb\u6808push\u3001\u51fa\u6808pop\u3001\u8bfb\u6808\u9876top\u7b49\u5e38\u7528\u64cd\u4f5c\u4ee5\u53ca\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\u7684isEmpty\u548c\u6808\u662f\u5426\u5df2\u6ee1\u7684isFu\u7b49\u8fb9\u754c\u5224\u65ad\u64cd\u4f5c\u3002\u6839\u636e\u62bd\u8c61\u548c\u5c01\u88c5\u7684\u539f\u5219,\u53ea\u53ef\u901a\u8fc7\u5176\u62bd\u8c61\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u7684\u8fd0\u7b97\u6765\u5bf9\u6808\u8fdb\u884c\u64cd\u4f5c\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e0b\u9762\u7684C&#043;&#043;\u7c7b\u6a21\u677f\u7ed9\u51fa\u4e86\u6808\u7c7b(\u540d\u5b57\u4e3a Stack,\u6a21\u677f\u53c2\u6570\u4e3a\u5143\u7d20\u7c7b\u578bT)\u7684\u4e00\u4e2a\u62bd\u8c61\u6570\u636e\u7c7b\u578b\u5b9a\u4e49\u3002<\/p>\n<p>\u3010\u4ee3\u78011.1\u3011\u6808\u7684\u62bd\u8c61\u6570\u636e\u7c7b\u578b\u5b9a\u4e49<\/p>\n<p>template &lt;class T&gt;                \/\/\u6808\u7684\u5143\u7d20\u7c7b\u578b\u4e3aT<br \/>\nclass Stack {<br \/>\npublic:                           \/\/\u6808\u7684\u8fd0\u7b97\u96c6<br \/>\n    void clear( );                \/\/\u53d8\u4e3a\u7a7a\u6808<br \/>\n    bool push(const T item);      \/\/item\u5165\u6808,\u6210\u529f\u5219\u8fd4\u56de\u771f,\u5426\u5219\u8fd4\u56de\u5047<br \/>\n    bool pop(T &amp; item);           \/\/\u8fd4\u56de\u6808\u9876\u5185\u5bb9\u5e76\u5f39\u51fa,\u6210\u529f\u8fd4\u56de\u771f,\u5426\u5219\u8fd4\u56de\u5047<br \/>\n    bool top(T &amp; item);           \/\/\u8fd4\u56de\u6808\u9876\u5185\u5bb9\u4f46\u4e0d\u5f39\u51fa,\u6210\u529f\u8fd4\u56de\u771f,\u5426\u5219\u8fd4\u56de\u5047<br \/>\n    bool isEmpty();               \/\/\u82e5\u6808\u5df2\u7a7a\u8fd4\u56de\u771f<br \/>\n    bool isFull();                \/\/\u82e5\u6808\u5df2\u6ee1\u8fd4\u56de\u771f<br \/>\n};<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e0a\u8ff0\u5b9a\u4e49\u7684\u8fd9\u4e2a\u62bd\u8c61\u6570\u636e\u7c7b\u578b\u5e76\u4e0d\u662f\u552f\u4e00\u7684\u3002\u9488\u5bf9\u5177\u4f53\u5e94\u7528\u7684\u4e0d\u540c\u8981\u6c42,\u64cd\u4f5c\u51fd\u6570\u53ef\u4ee5\u9002\u5f53\u589e\u5220\u3002\u4f8b\u5982,\u6808\u7684\u94fe\u8868\u5b9e\u73b0\u4e2d\u5e76\u4e0d\u9700\u8981\u5224\u65ad\u6808\u662f\u5426\u4e3a\u6ee1,\u67d0\u4e9b\u5b9e\u73b0\u4e2d\u4e5f\u8bb8\u4f1a\u628a\u8bfb\u53d6\u6808\u9876\u5143\u7d20\u7684top\u64cd\u4f5c\u548c\u51fa\u6808\u64cd\u4f5cpop\u5408\u4e8c\u4e3a\u4e00\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6808\u7684\u5b9e\u73b0\u4e0e\u5176\u5b58\u50a8\u7ed3\u6784\u76f8\u5173,\u4e0b\u9762\u4ecb\u7ecd\u987a\u5e8f\u6808\u548c\u94fe\u5f0f\u6808\u4e24\u79cd\u5b58\u50a8\u7ed3\u6784\u53ca\u76f8\u5e94\u7684\u6808\u64cd\u4f5c.<\/p>\n<h3 id=\"1.1.2%20%E9%A1%BA%E5%BA%8F%E6%A0%88\">1.1.2 \u987a\u5e8f\u6808<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u91c7\u7528\u987a\u5e8f\u5b58\u50a8\u7ed3\u6784\u7684\u6808\u79f0\u4e3a\u987a\u5e8f\u6808(array-basedstack),\u9700\u8981\u4e00\u5757\u8fde\u7eed\u7684\u533a\u57df\u6765\u5b58\u50a8\u6808\u4e2d\u7684\u5143\u7d20,\u56e0\u6b64\u9700\u8981\u4e8b\u5148\u77e5\u9053\u6216\u4f30\u7b97\u6808\u7684\u5927\u5c0f\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u987a\u5e8f\u6808\u672c\u8d28\u4e0a\u662f\u7b80\u5316\u7684\u987a\u5e8f\u8868\u3002\u5bf9\u5143\u7d20\u6570\u76ee\u4e3an\u7684\u6808,\u9996\u5148\u9700\u8981\u786e\u5b9a\u6570\u7ec4\u7684\u54ea\u4e00\u7aef\u8868\u793a\u6808\u9876\u3002\u5982\u679c\u628a\u6570\u7ec4\u7684\u7b2c0\u4e2a\u4f4d\u7f6e\u4f5c\u4e3a\u6808\u9876,\u6309\u7167\u6808\u7684\u5b9a\u4e49,\u6240\u6709\u7684\u63d2\u4eba\u548c\u5220\u9664\u64cd\u4f5c\u90fd\u5728\u7b2c0\u4e2a\u4f4d\u7f6e\u4e0a\u8fdb\u884c,\u5373\u610f\u5473\u7740\u6bcf\u6b21\u7684push\u6216pop\u64cd\u4f5c\u90fd\u9700\u8981\u628a\u5f53\u524d\u6808\u7684\u6240\u6709\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u540e\u79fb\u6216\u524d\u79fb\u4e00\u4e2a\u4f4d\u7f6e,\u65f6\u95f4\u4ee3\u4ef7\u4e3a0(n)\u3002\u53cd\u4e4b,\u5982\u679c\u628a\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e-1\u4f5c\u4e3a\u6808\u9876,\u90a3\u4e48\u53ea\u9700\u5c06\u65b0\u5143\u7d20\u6dfb\u52a0\u5728\u8868\u5c3e,\u51fa\u6808\u64cd\u4f5c\u4e5f\u53ea\u9700\u5220\u9664\u8868\u5c3e\u5143\u7d20,\u6bcf\u6b21\u64cd\u4f5c\u7684\u65f6\u95f4\u4ee3\u4ef7\u4ec5\u4e3a0(1)\u3002\u56fe3.2\u6240\u793a\u4e3a\u6309\u540e\u4e00\u79cd\u65b9\u6848\u5b9e\u73b0\u7684\u6808,\u5176\u4e2dt\u8868\u793a\u6808\u9876\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"187\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014850-682fd4025029a.png\" width=\"1462\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u987a\u5e8f\u6808\u5b9e\u73b0\u65f6,\u7528\u4e00\u4e2a\u6574\u578b\u53d8\u91cftop(\u901a\u5e38\u79f0\u4e3a\u6808\u9876\u6307\u9488)\u6765\u6307\u793a\u5f53\u524d\u6808\u9876\u4f4d\u7f6e,\u540c\u65f6\u4e5f\u53ef\u4ee5\u8868\u793a\u5f53\u524d\u6808\u4e2d\u5143\u7d20\u7684\u4e2a\u6570\u3002\u4ee3\u78011.2\u7ed9\u51fa\u4e86\u4e00\u4e2a\u987a\u5e8f\u6808\u7c7b\u53ca\u5176\u90e8\u5206\u6210\u5458\u51fd\u6570\u7684\u5b9e\u73b0\u65b9\u6cd5\u3002<\/p>\n<p>\u3010\u4ee3\u78011.2\u3011\u6808\u7684\u987a\u5e8f\u5b9e\u73b0<\/p>\n<p>template &lt;class T&gt;<br \/>\nclass arrStack: public Stack &lt;T&gt; {<br \/>\nprivate:                        \/\/\u6808\u7684\u987a\u5e8f\u5b58\u50a8<br \/>\n    int mSize;                  \/\/\u6808\u4e2d\u6700\u591a\u53ef\u5b58\u653e\u7684\u5143\u7d20\u4e2a\u6570<br \/>\n    int top;                    \/\/\u6808\u9876\u4f4d\u7f6e,\u5e94\u5c0f\u4e8emSize<br \/>\n    T   *st                     \/\/\u5b58\u653e\u6808\u5143\u7d20\u7684\u6570\u7ec4<br \/>\npublic:                         \/\/\u6808\u7684\u8fd0\u7b97\u7684\u987a\u5e8f\u5b9e\u73b0<br \/>\n    arrStack(int size) {        \/\/\u521b\u5efa\u4e00\u4e2a\u7ed9\u5b9a\u957f\u5ea6\u7684\u987a\u5e8f\u6808\u5b9e\u4f8b<br \/>\n        mSize &#061; size;<br \/>\n        top &#061; -l;<br \/>\n        st &#061; new T[mSize];<br \/>\n    }<br \/>\n    arrStack() {                \/\/\u521b\u5efa\u4e00\u4e2a\u987a\u5e8f\u6808\u7684\u5b9e\u4f8b<br \/>\n        top &#061; -l;<br \/>\n    }<br \/>\n    ~arrStack() {               \/\/\u6790\u6784\u51fd\u6570<br \/>\n        delete [] st;<br \/>\n    }<br \/>\n    void clear() {              \/\/\u6e05\u7a7a\u6808\u5185\u5bb9<br \/>\n        top &#061; -1;<br \/>\n    }<br \/>\n    bool push (const T item) {  \/\/\u5165\u6808\u64cd\u4f5c\u7684\u987a\u5e8f\u5b9e\u73b0<br \/>\n        if(top &#061;&#061; mSize-1) {    \/\/\u6808\u5df2\u6ee1<br \/>\n            cout&lt;&lt; &#034;\u6808\u6ee1\u6ea2\u51fa&#034; &lt;&lt; endl;<br \/>\n            return false;<br \/>\n        }<br \/>\n        else {                  \/\/\u65b0\u5143\u7d20\u4eba\u6808\u5e76\u4fee\u6539\u6808\u9876\u6307\u9488<br \/>\n            st[&#043;&#043;top] &#061; item;<br \/>\n            return true;<br \/>\n        }<br \/>\n    }<br \/>\n    bool pop(T &amp; item) {        \/\/\u51fa\u6808\u7684\u987a\u5e8f\u5b9e\u73b0<br \/>\n        if(top &#061;&#061; -1) {         \/\/\u6808\u4e3a\u7a7a<br \/>\n            cout &lt;&lt; &#034;\u6808\u4e3a\u7a7a,\u4e0d\u80fd\u6267\u884c\u51fa\u6808\u64cd\u4f5c&#034; &lt;&lt; endl;<br \/>\n            return false;<br \/>\n        }<br \/>\n        else {<br \/>\n            item &#061; st[top&#8211;];   \/\/\u8fd4\u56de\u6808\u9876\u5143\u7d20\u5e76\u4fee\u6539\u6808\u9876\u6307\u9488<br \/>\n            return true;<br \/>\n        }<br \/>\n    }<br \/>\n    bool top(T &amp; item) {        \/\/\u8fd4\u56de\u6808\u9876\u5185\u5bb9,\u4f46\u4e0d\u5f39\u51fa<br \/>\n        if(top &#061;&#061; -1) {         \/\/\u6808\u7a7a<br \/>\n            cout &lt;&lt; &#034;\u6808\u4e3a\u7a7a,\u4e0d\u80fd\u8bfb\u53d6\u6808\u9876\u5143\u7d20&#034; &lt;&lt; endl;<br \/>\n            return false;<br \/>\n        }<br \/>\n        else {<br \/>\n            item &#061; st[top];<br \/>\n            return true;<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0top\u53ef\u4ee5\u6709\u4e24\u79cd\u5b9a\u4e49\u65b9\u5f0f\u3002\u4e00\u79cd\u662f\u5c06\u5176\u8bbe\u7f6e\u4e3a\u6808\u4e2d\u7b2c\u4e00\u4e2a\u7a7a\u95f2\u4f4d\u7f6e,\u5373\u7a7a\u6808\u7684top\u4e3a0;\u53e6\u4e00\u79cd\u5219\u628a top \u5b9a\u4e49\u4e3a\u6808\u4e2d\u6700\u4e0a\u9762\u7684\u90a3\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e,\u800c\u975e\u7b2c\u4e00\u4e2a\u7a7a\u95f2\u4f4d\u7f6e,\u6b64\u65f6\u7a7a\u6808\u7684 top \u5e94\u8be5\u521d\u59cb\u5316\u4e3a-1\u6216\u4efb\u4f55\u975e\u81ea\u7136\u6570\u3002\u5728\u672c\u4e66\u4e2d\u91c7\u53d6\u540e\u8005\u6765\u8868\u793a\u6808\u9876\u3002\u56e0\u4e3a\u524d\u8005\u4f1a\u6d6a\u8d39\u6570\u7ec4\u7684\u4e00\u4e2a\u4f4d\u7f6e\u3002\u8fd9\u6837,push \u548c pop \u64cd\u4f5c\u53ea\u5728 top \u6240\u6307\u793a\u7684\u6570\u7ec4\u4f4d\u7f6e\u63d2\u4eba\u6216\u5220\u9664\u4e00\u4e2a\u5143\u7d20\u3002\u56e0\u4e3a top \u8868\u793a\u6808\u4e2d\u6700\u4e0a\u9762\u5143\u7d20\u7684\u4f4d\u7f6e,\u6240\u4ee5 push \u4e00\u4e2a\u5143\u7d20\u65f6\u9996\u5148\u628atop \u52a01,\u7136\u540e\u628a\u65b0\u5143\u7d20\u63d2\u5165\u5230\u65b0\u7684\u6808\u9876\u4f4d\u7f6e\u3002\u540c\u6837,pop\u5148\u5220\u9664\u6808\u9876\u5143\u7d20,\u518d\u628atop \u51cf1\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u56fe3.3\u6240\u793a\u4e3amSize\u53d6\u503c\u4e3a6\u7684\u987a\u5e8f\u6808s\u4e2d\u6570\u636e\u5143\u7d20\u548c\u6808\u9876\u6307\u9488\u7684\u53d8\u5316\u3002\u5176\u4e2d,\u56fe3.3(a)\u8868\u793a\u7a7a\u6808\u72b6\u6001,\u6b64\u65f6stop&#061;-1;\u56fe3.3(b)\u8868\u793a\u6808\u4e2d\u5177\u6709\u4e00\u4e2a\u5143\u7d20\u7684\u60c5\u51b5,\u6b64\u65f6\u6808\u9876top \u6240\u6307\u4e3a\u6808\u9876\u5143\u7d20\u6240\u5728\u7684\u4f4d\u7f6e0;\u56fe3.3(c)\u5219\u8868\u793a\u5728\u8fdb\u884c\u82e5\u5e72\u6b21\u8fdb\u6808\u64cd\u4f5c\u4e4b\u540e\u6808\u6ee1\u7684\u60c5\u51b5,\u6b64\u65f6 top&#061;mSize-1:\u56fe3.3(d)\u5219\u662f\u5728\u56fe3.3(c)\u57fa\u7840\u4e0a\u8fde\u7eed\u51fa\u6808\u4e24\u6b21\u540e\u7684\u72b6\u6001\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6808\u4e2d\u5143\u7d20\u662f\u52a8\u6001\u53d8\u5316\u7684,\u5f53\u6808\u4e2d\u5df2\u6709mSize\u4e2a\u5143\u7d20\u65f6,\u8fdb\u6808\u64cd\u4f5c\u4f1a\u4ea7\u751f\u4e0a\u6ea2\u51fa(overflow)\u3002\u76f8\u5e94\u7684,\u5728\u7a7a\u6808\u4e0a\u8fdb\u884c\u51fa\u6808\u64cd\u4f5c\u5219\u4f1a\u9020\u6210\u4e0b\u6ea2\u51fa(underflow)\u3002\u4e3a\u4e86\u907f\u514d\u6ea2\u51fa,\u5728\u5bf9\u6808\u8fdb\u884c push\u548c pop \u64cd\u4f5c\u4e4b\u524d\u9700\u8981\u68c0\u67fb\u6808\u662f\u5426\u5df2\u6ee1\u6216\u662f\u5426\u5df2\u7a7a\u3002\u00a0<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"578\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014850-682fd40278c6a.png\" width=\"1472\" \/><\/p>\n<p>\u00a0\u3010\u7b97\u6cd51.3\u3011\u6539\u8fdb\u7684\u8fdb\u6808\u64cd\u4f5c<\/p>\n<p>    template &lt;class T&gt;<br \/>\n    bool arrStack&lt;T&gt; :: push(const T item) {<br \/>\n        if(top &#061;&#061; mSize -1) {<br \/>\n            T *newSt &#061; newT[mSize*2];<br \/>\n            for(i &#061; 0; i &lt;&#061; top; i&#043;&#043;)<br \/>\n                newSt[i] &#061; st[i};<br \/>\n            delete [] st;            \/\/\u91ca\u653e\u539f\u6808<br \/>\n            st &#061; newSt;<br \/>\n            mSize *&#061; 2;<br \/>\n        }<br \/>\n        st[&#043;&#043;top] &#061; item;<br \/>\n        return true;<br \/>\n    } <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5982\u679c\u51fa\u73b0\u4e0a\u6ea2\u65f6\u4ecd\u5e0c\u671b\u5bf9\u987a\u5e8f\u6808\u6267\u884c\u8fdb\u6808\u64cd\u4f5c,\u53ef\u4ee5\u8003\u8651\u5bf9\u5f53\u524d\u7684\u987a\u5e8f\u6808\u8fdb\u884c\u9002\u5f53\u7684\u6269\u5bb9\u3002\u4f8b\u5982,\u50cf\u7b97\u6cd51.3\u90a3\u6837,\u7533\u8bf7\u4e00\u4e2a\u6269\u5927\u4e00\u500d\u7684\u65b0\u6570\u7ec4,\u628a\u987a\u5e8f\u6808\u7684\u539f\u6709\u5185\u5bb9\u987a\u5e8f\u79fb\u52a8\u5230\u65b0\u7684\u6570\u7ec4\u4e2d,\u518d\u6309\u6b63\u5e38\u7684\u65b9\u5f0f\u6765\u6267\u884c\u8fdb\u6808\u64cd\u4f5c\u3002\u00a0<\/p>\n<h3 id=\"1.1.3%20%E9%93%BE%E5%BC%8F%E6%A0%88\" style=\"background-color:transparent\">1.1.3 \u94fe\u5f0f\u6808<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u94fe\u5f0f\u6808(linked stack)\u672c\u8d28\u4e0a\u662f\u7b80\u5316\u7684\u94fe\u8868\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u4e3a\u4e86\u65b9\u4fbf\u5b58\u53d6,\u6808\u9876\u5143\u7d20\u5e94\u8be5\u8bbe\u7f6e\u4e3a\u94fe\u8868\u5934\u3002\u56fe3.4\u6240\u793a\u4e3a\u94fe\u5f0f\u6808\u7684\u4e00\u4e2a\u7b80\u5355\u793a\u610f\u56fe\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4ee3\u78011.4\u662f\u94fe\u5f0f\u6808\u7684\u4e00\u4e2a\u7b80\u5355\u5b9e\u73b0\u3002\u5176\u4e2d,\u6570\u636e\u6210\u5458top\u662f\u4e00\u4e2a\u6307\u5411\u94fe\u5f0f\u6808\u7684\u9996\u7ed3\u70b9(\u6808\u9876)\u7684\u6307\u9488,\u94fe\u8868\u7684\u7ed3\u70b9\u7c7b\u578b\u91c7\u7528\u7b2c2\u7ae0\u4e2d\u5df2\u5b9a\u4e49\u8fc7\u7684Link\u7c7b\u6a21\u677f\u3002\u8fdb\u6808\u64cd\u4f5cpush \u5728\u94fe\u8868\u5934\u63d2\u5165\u5143\u7d20,\u51fa\u6808\u64cd\u4f5cpop\u5220\u9664\u94fe\u5934\u5143\u7d20\u5e76\u91ca\u653e\u7a7a\u95f4\u3002\u663e\u800c\u6613\u89c1,push\u548cpop\u7684\u65f6\u95f4\u4ee3\u4ef7\u5747\u4e3aO(1)\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"237\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014850-682fd402bd348.png\" width=\"1511\" \/><\/p>\n<p>\u3010\u4ee3\u78011.4\u3011\u6808\u7684\u94fe\u5f0f\u5b9e\u73b0<\/p>\n<p>template &lt;ciass T&gt;<br \/>\nclass lnkStack : public Stack &lt;T&gt; {<br \/>\nprivate:                                \/\/\u6808\u7684\u94fe\u5f0f\u5b58\u50a8<br \/>\n    Link&lt;T&gt;* top;                       \/\/\u6307\u5411\u6808\u9876\u7684\u6307\u9488<br \/>\n    int size;                           \/\/\u5b58\u653e\u5143\u7d20\u7684\u4e2a\u6570<br \/>\npublic:                                 \/\/\u6808\u8fd0\u7b97\u7684\u94fe\u5f0f\u5b9e\u73b0<br \/>\n    lnkStack(int defSize) {             \/\/\u6784\u9020\u51fd\u6570<br \/>\n        top &#061; NULL;<br \/>\n        size &#061; 0;<br \/>\n    }<br \/>\n    ~lnkStack() {                       \/\/\u6790\u6784\u51fd\u6570<br \/>\n        clear();<br \/>\n    }<br \/>\n    void clear() {                      \/\/\u6e05\u7a7a\u6808\u5185\u5bb9<br \/>\n        while(top!&#061;NULL) {<br \/>\n            Link&lt;T&gt; * tmp &#061; top;<br \/>\n            top &#061; top -&gt; next;<br \/>\n            delete tmp;<br \/>\n        }<br \/>\n        size &#061; 0;<br \/>\n    }<br \/>\n    bool push(const T item) {            \/\/\u5165\u6808\u64cd\u4f5c\u7684\u94fe\u5f0f\u5b9e\u73b0<br \/>\n        Link&lt;T&gt; * tmp &#061; new Link&lt;T&gt;(item,top);<br \/>\n        top &#061; tmp ;<br \/>\n        size&#043;&#043;;<br \/>\n        return true;<br \/>\n    }<br \/>\n    bool pop(T&amp; item) {                  \/\/\u51fa\u6808\u7684\u94fe\u5f0f\u5b9e\u73b0<br \/>\n        Link&lt;T&gt; * tmp;<br \/>\n        if(size &#061;&#061; 0) {<br \/>\n            cout &lt;&lt; &#034;\u6808\u4e3a\u7a7a,\u4e0d\u80fd\u6267\u884c\u51fa\u6808\u64cd\u4f5c&#034; &lt; &lt;endl;<br \/>\n            return false;<br \/>\n        }<br \/>\n        item &#061; top -&gt; data;<br \/>\n        tmp &#061; top-&gt;next;<br \/>\n        delete top;<br \/>\n        top &#061; tmp;<br \/>\n        size&#8211;;<br \/>\n        return true;<br \/>\n    }<br \/>\n    bool top(T &amp; item) {                 \/\/\u8fd4\u56de\u6808\u9876\u5185\u5bb9,\u4f46\u4e0d\u5f39\u51fa<br \/>\n        if(size &#061;&#061; 0) {<br \/>\n            cout &lt;&lt; &#034;\u6808\u4e3a\u7a7a,\u4e0d\u80fd\u8bfb\u53d6\u6808\u9876\u5143\u7d20&#034; &lt;&lt; endl;<br \/>\n            return false;<br \/>\n        }<br \/>\n        item &#061; top -&gt; data;<br \/>\n        return true;<br \/>\n    }<br \/>\n} <\/p>\n<h2 id=\"1.2%C2%A0%E6%A0%88%E4%B8%8E%E9%80%92%E5%BD%92\">1.2\u00a0\u6808\u4e0e\u9012\u5f52<\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7531\u4e8e\u7b26\u5408\u4eba\u7c7b\u81ea\u9876\u5411\u4e0b\u62bd\u8c61\u63cf\u8ff0\u95ee\u9898\u7684\u601d\u7ef4\u65b9\u5f0f,\u9012\u5f52\u6210\u4e3a\u6570\u5b66\u548c\u8ba1\u7b97\u673a\u79d1\u5b66\u7684\u57fa\u672c\u6982\u5ff5\u662f\u89e3\u51b3\u590d\u6742\u95ee\u9898\u7684\u4e00\u4e2a\u6709\u529b\u624b\u6bb5\u3002\u8bb8\u591a\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u90fd\u652f\u6301\u9012\u5f52,\u8fd9\u4e9b\u652f\u6301\u672c\u8d28\u4e0a\u662f\u901a\u8fc7\u6808\u6765\u5b9e\u73b0\u7684\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u672c\u8282\u5c06\u4ee5\u9636\u4e58\u51fd\u6570\u7684\u8ba1\u7b97\u4e3a\u4f8b,\u5206\u6790\u51fd\u6570\u7684\u9012\u5f52\u8c03\u7528\u5728\u7a0b\u5e8f\u8fd0\u884c\u9636\u6bb5\u7684\u5de5\u4f5c\u8fc7\u7a0b\u3002\u5728\u6b64\u57fa\u7840\u4e0a,\u4ee5\u80cc\u5305\u95ee\u9898\u4e3a\u4f8b\u8bf4\u660e\u9012\u5f52\u7b97\u6cd5\u5230\u975e\u9012\u5f52\u7684\u4e00\u79cd\u673a\u68b0\u8f6c\u6362\u65b9\u6cd5\u3002<\/p>\n<h3 id=\"1.2.1%20%E9%80%92%E5%BD%92%E7%9A%84%E5%AE%9A%E4%B9%89\">1.2.1 \u9012\u5f52\u7684\u5b9a\u4e49<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4ee5\u9636\u4e58\u51fd\u6570\u4e3a\u4f8b\u6765\u8bf4\u660e\u9012\u5f52\u7684\u5b9a\u4e49\u3002\u9636\u4e58n!\u7684\u9012\u5f52\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"94\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014850-682fd402e0fc3.png\" width=\"1266\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e3a\u4e86\u5b9a\u4e49\u6574\u6570n\u7684\u9636\u4e58,\u5fc5\u987b\u5148\u5b9a\u4e49(n-1)\u7684\u9636\u4e58,\u800c\u4e3a\u4e86\u5b9a\u4e49(n-1)\u7684\u9636\u4e58,\u53c8\u9700\u5148\u5b9a\u4e49(n-2)\u7684\u9636\u4e58,\u5982\u6b64\u76f4\u52300\u4e3a\u6b62,\u56e0\u4e3a\u6b64\u65f6\u9636\u4e58\u5b9a\u4e49\u4e3a1\u3002\u8fd9\u79cd\u7528\u81ea\u8eab\u7684\u7b80\u5355\u60c5\u51b5\u6765\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u5b9a\u4e49\u81ea\u5df1\u7684\u65b9\u5f0f\u79f0\u4e3a\u9012\u5f52\u5b9a\u4e49\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53ef\u4ee5\u770b\u51fa,\u4e00\u4e2a\u9012\u5f52\u5b9a\u4e49\u7531\u4e24\u90e8\u5206\u7ec4\u6210\u3002\u5176\u4e00\u4e3a\u9012\u5f52\u57fa\u7840,\u4e5f\u79f0\u9012\u5f52\u51fa\u53e3,\u662f\u9012\u5f52\u5b9a\u4e49\u7684\u6700\u57fa\u672c\u60c5\u51b5,\u4e5f\u662f\u4fdd\u8bc1\u9012\u5f52\u7ed3\u675f\u7684\u524d\u63d0;\u5176\u4e8c\u5219\u4e3a\u9012\u5f52\u89c4\u5219,\u786e\u5b9a\u4e86\u7531\u7b80\u5355\u60c5\u51b5\u6784\u7b51\u590d\u6742\u60c5\u51b5\u9700\u9075\u5faa\u7684\u89c4\u5219\u3002\u4e0a\u9762\u7684\u9636\u4e58\u5b9a\u4e49\u4e2d\u9012\u5f52\u51fa\u53e3\u5373\u4e3an\u22640,\u6b64\u65f6\u9636\u4e58\u5b9a\u4e49\u4e3a1;\u9012\u5f52\u89c4\u5219\u4e3anx(n-1)!\u5373n\u7684\u9636\u4e58\u7531(n-1)\u7684\u9636\u4e58\u6765\u6784\u7b51\u3002\u8fd9\u4e2a\u9012\u5f52\u5b9a\u4e49\u53ef\u5728C&#043;&#043;\u8bed\u8a00\u4e2d\u7531\u4e0b\u9762\u7684\u8fd9\u4e2a\u9012\u5f52\u51fd\u6570\u6765\u5b9e\u73b0:\u00a0<\/p>\n<p>\u3010\u7b97\u6cd51.6\u3011\u9636\u4e58\u51fd\u6570<\/p>\n<p>long factorial(long n) {<br \/>\n    if(n &lt;&#061; 0)<br \/>\n        return 1;<br \/>\n    return n *factorial(n-l);<br \/>\n}<\/p>\n<h3 id=\"1.2.2%20%E9%80%92%E5%BD%92%E5%87%BD%E6%95%B0%E7%9A%84%E5%AE%9E%E7%8E%B0\" style=\"background-color:transparent\">1.2.2 \u9012\u5f52\u51fd\u6570\u7684\u5b9e\u73b0<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5927\u591a\u6570\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u8fd0\u884c\u73af\u5883\u6240\u63d0\u4f9b\u7684\u51fd\u6570\u8c03\u7528\u673a\u5236\u662f\u7531\u5e95\u5c42\u7684\u7f16\u8bd1\u6808\u652f\u6301\u7684\u3002\u7f16\u8bd1\u6808\u4e2d\u7684\u201c\u8fd0\u884c\u65f6\u73af\u5883\u201d\u6307\u7684\u662f\u76ee\u6807\u8ba1\u7b97\u673a\u4e0a\u7528\u6765\u7ba1\u7406\u5b58\u50a8\u5668\u5e76\u4fdd\u5b58\u6267\u884c\u8fc7\u7a0b\u6240\u9700\u4fe1\u606f\u7684\u5bc4\u5b58\u5668\u53ca\u5b58\u50a8\u5668\u7684\u7ed3\u6784\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5728\u975e\u9012\u5f52\u8c03\u7528\u7684\u60c5\u51b5\u4e0b,\u6570\u636e\u533a\u7684\u5206\u914d\u53ef\u4ee5\u5728\u7a0b\u5e8f\u8fd0\u884c\u524d\u8fdb\u884c,\u76f4\u5230\u6574\u4e2a\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u675f\u518d\u91ca\u653e,\u8fd9\u79cd\u5206\u914d\u79f0\u4e3a\u9759\u6001\u5206\u914d\u3002\u91c7\u7528\u9759\u6001\u5206\u914d\u65f6,\u51fd\u6570\u7684\u8c03\u7528\u548c\u8fd4\u56de\u5904\u7406\u6bd4\u8f83\u7b80\u5355,\u4e0d\u9700\u8981\u6bcf\u6b21\u5206\u914d\u548c\u91ca\u653e\u88ab\u8c03\u7528\u51fd\u6570\u7684\u6570\u636e\u533a\u3002\u5728\u9012\u5f52\u8c03\u7528\u7684\u60c5\u51b5\u4e0b,\u88ab\u8c03\u51fd\u6570\u7684\u5c40\u90e8\u53d8\u91cf\u4e0d\u80fd\u9759\u6001\u5730\u5206\u914d\u67d0\u4e9b\u56fa\u5b9a\u5355\u5143,\u800c\u5fc5\u987b\u6bcf\u8c03\u7528\u4e00\u6b21\u5c31\u5206\u914d\u4e00\u4efd,\u4ee5\u5b58\u653e\u5f53\u524d\u6240\u4f7f\u7528\u7684\u6570\u636e,\u5f53\u8fd4\u56de\u65f6\u968f\u5373\u91ca\u653e\u3002\u8fd9\u79cd\u53ea\u6709\u5728\u6267\u884c\u8c03\u7528\u65f6\u624d\u80fd\u8fdb\u884c\u7684\u5b58\u50a8\u5206\u914d\u79f0\u4e3a\u201c\u52a8\u6001\u5206\u914d\u201d,\u6b64\u65f6\u9700\u8981\u5728\u5185\u5b58\u4e2d\u5f00\u8f9f\u4e00\u4e2a\u79f0\u4e3a\u8fd0\u884c\u6808(runtime stack)\u7684\u8db3\u591f\u5927\u7684\u52a8\u6001\u533a\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7528\u4f5c\u52a8\u6001\u6570\u636e\u5206\u914d\u7684\u5b58\u50a8\u533a\u53ef\u6309\u591a\u79cd\u65b9\u5f0f\u7ec4\u7ec7\u3002\u5178\u578b\u7684\u7ec4\u7ec7\u5982\u56fe3.6\u6240\u793a,\u5c06\u5b58\u50a8\u5668\u5206\u4e3a\u6808(stack)\u533a\u57df\u548c\u5806(heap)\u533a\u57df,\u6808\u533a\u57df\u7528\u4e8e\u5206\u914d\u5177\u6709\u540e\u8fdb\u5148\u51faLIFO\u7279\u5f81\u4e2d\u7684\u6570\u636e(\u5982\u51fd\u6570\u7684\u8c03\u7528),\u800c\u5806\u533a\u57df\u5219\u7528\u4e8e\u4e0d\u7b26\u5408LIFO\u7684\u6570\u636e(\u5982\u6307\u9488\u7684\u5206\u914d)\u7684\u52a8\u6001\u5206\u914d\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8fd0\u884c\u6808\u4e2d\u5143\u7d20\u7684\u7c7b\u578b(\u5373\u88ab\u8c03\u51fd\u6570\u9700\u8981\u7684\u6570\u636e\u533a\u7c7b\u578b)\u6d89\u53ca\u52a8\u6001\u5b58\u50a8\u5206\u914d\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u5ff5:\u51fd\u6570\u6d3b\u52a8\u8bb0\u5f55(activationrecord)\u3002\u5f53\u8c03\u7528\u6216\u6fc0\u6d3b\u4e00\u4e2a\u51fd\u6570\u65f6,\u76f8\u5e94\u7684\u6d3b\u52a8\u8bb0\u5f55\u5305\u542b\u4e86\u4e3a\u8be5\u51fd\u6570\u7684\u5c40\u90e8\u6570\u636e\u6240\u5206\u914d\u7684\u5b58\u50a8\u7a7a\u95f4\u3002\u901a\u5e38,\u6d3b\u52a8\u8bb0\u5f55\u81f3\u5c11\u5e94\u5305\u62ec\u5982\u56fe3.7\u6240\u793a\u7684\u51e0\u90e8\u5206\u5185\u5bb9\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"372\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014851-682fd40301e52.png\" width=\"1262\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6bcf\u6b21\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u65f6,\u6267\u884c\u8fdb\u6808\u64cd\u4f5c,\u628a\u88ab\u8c03\u51fd\u6570\u6240\u5bf9\u5e94\u7684\u6d3b\u52a8\u8bb0\u5f55\u5206\u914d\u5728\u6808\u7684\u9876\u90e8;\u800c\u5728\u6bcf\u6b21\u4ece\u51fd\u6570\u8fd4\u56de\u65f6,\u6267\u884c\u51fa\u6808\u64cd\u4f5c,\u91ca\u653e\u672c\u6b21\u7684\u6d3b\u52a8\u8bb0\u5f55,\u6062\u590d\u5230\u4e0a\u6b21\u8c03\u7528\u6240\u5206\u914d\u7684\u6570\u636e\u533a\u4e2d\u3002\u56e0\u6b64,\u88ab\u8c03\u51fd\u6570\u4e2d\u53d8\u91cf\u5730\u5740\u5168\u90e8\u91c7\u7528\u76f8\u5bf9\u4e8e\u6808\u9876\u7684\u76f8\u5bf9\u5730\u5740\u6765\u8868\u793a\u3002\u56e0\u4e3a\u8fd0\u884c\u6808\u4e2d\u5b58\u653e\u7684\u662f\u88ab\u8c03\u51fd\u6570\u7684\u6d3b\u52a8\u8bb0\u5f55,\u6240\u4ee5\u8fd0\u884c\u6808\u53c8\u79f0\u4e3a\u6d3b\u52a8\u8bb0\u5f55\u6808(stack of activation record)\u3002\u540c\u65f6,\u7531\u4e8e\u8fd0\u884c\u6808\u6309\u7167\u51fd\u6570\u7684\u8c03\u7528\u5e8f\u5217\u6765\u7ec4\u7ec7,\u56e0\u6b64\u4e5f\u79f0\u4e3a\u8c03\u7528\u6808(callstack)\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e00\u4e2a\u51fd\u6570\u5728\u8fd0\u884c\u6808\u4e2d\u53ef\u4ee5\u6709\u82e5\u5e72\u4e0d\u540c\u7684\u6d3b\u52a8\u8bb0\u5f55,\u6bcf\u4e2a\u4ee3\u8868\u4e86\u4e00\u4e2a\u4e0d\u540c\u7684\u8c03\u7528\u3002\u5bf9\u4e8e\u9012\u5f52\u51fd\u6570\u6765\u8bf4,\u9012\u5f52\u6df1\u5ea6\u5c31\u51b3\u5b9a\u4e86\u5176\u5728\u8fd0\u884c\u6808\u4e2d\u6d3b\u52a8\u8bb0\u5f55\u7684\u6570\u76ee\u3002\u5f53\u51fd\u6570\u8fdb\u884c\u9012\u5f52\u8c03\u7528\u65f6,\u51fd\u6570\u4f53\u7684\u540c\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u5728\u4e0d\u540c\u7684\u9012\u5f52\u5c42\u6b21\u88ab\u5206\u914d\u7ed9\u4e0d\u540c\u7684\u5b58\u50a8\u7a7a\u95f4,\u653e\u5728\u8fd0\u884c\u6808\u7684\u4e0d\u540c\u4f4d\u7f6e\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6982\u62ec\u6765\u8bb2,\u51fd\u6570\u8c03\u7528\u53ef\u4ee5\u5206\u89e3\u6210\u4ee5\u4e0b3\u6b65\u6765\u5b9e\u73b0:<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(1)\u8c03\u7528\u51fd\u6570\u53d1\u9001\u8c03\u7528\u4fe1\u606f,\u5305\u62ec\u8c03\u7528\u65b9\u8981\u4f20\u9001\u7ed9\u88ab\u8c03\u65b9\u7684\u4fe1\u606f,\u5982\u4f20\u7ed9\u5f62\u5f0f\u53c2\u6570(\u7b80\u79f0\u5f62\u53c2)\u7684\u5b9e\u5728\u53c2\u6570(\u7b80\u79f0\u5b9e\u53c2)\u7684\u503c\u3001\u51fd\u6570\u8fd4\u56de\u5730\u5740\u7b49\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(2)\u5206\u914d\u88ab\u8c03\u65b9\u9700\u8981\u7684\u5c40\u90e8\u6570\u636e\u533a,\u7528\u6765\u5b58\u653e\u88ab\u8c03\u65b9\u5b9a\u4e49\u7684\u5c40\u90e8\u53d8\u91cf\u3001\u5f62\u53c2\u53d8\u91cf(\u5b58\u653e\u5b9e\u53c2\u7684\u503c)\u8fd4\u56de\u5730\u5740\u7b49,\u5e76\u63a5\u6536\u8c03\u7528\u65b9\u4f20\u9001\u6765\u7684\u8c03\u7528\u4fe1\u606f\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(3)\u8c03\u7528\u65b9\u6682\u505c,\u628a\u8ba1\u7b97\u63a7\u5236\u8f6c\u79fb\u5230\u88ab\u8c03\u65b9,\u5373\u81ea\u52a8\u8f6c\u79fb\u5230\u88ab\u8c03\u51fd\u6570\u7684\u7a0b\u5e8f\u5165\u53e3\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5f53\u88ab\u8c03\u65b9\u7ed3\u675f\u8fd0\u884c,\u8fd4\u56de\u5230\u8c03\u7528\u65b9\u65f6,\u5176\u8fd4\u56de\u5904\u7406\u4e00\u822c\u4e5f\u5206\u89e3\u4e3a3\u6b65\u8fdb\u884c:<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(1)\u4f20\u9001\u8fd4\u56de\u4fe1\u606f,\u5305\u62ec\u88ab\u8c03\u65b9\u8981\u4f20\u56de\u7ed9\u8c03\u7528\u65b9\u7684\u4fe1\u606f,\u8bf8\u5982\u8ba1\u7b97\u7ed3\u679c\u7b49\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(2)\u91ca\u653e\u5206\u914d\u7ed9\u88ab\u8c03\u65b9\u7684\u6570\u636e\u533a\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(3)\u6309\u8fd4\u56de\u5730\u5740\u628a\u63a7\u5236\u8f6c\u56de\u8c03\u7528\u65b9\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8bbe\u8981\u8ba1\u7b974\u7684\u9636\u4e58,\u5728C\/C&#043;&#043;\u8bed\u8a00\u4e2d\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a\u4e3b\u7a0b\u5e8fmain(\u53c2\u8003\u7b97\u6cd5 1.7)\u6765\u8c03\u7528\u7b97\u6cd51.6\u5b9a\u4e49\u7684\u9636\u4e58\u51fd\u6570\u3002<\/p>\n<p>\u3010\u7b97\u6cd51.7\u3011\u8ba1\u7b97\u9636\u4e58\u7684\u4e3b\u7a0b\u5e8f<\/p>\n<p>#include &lt;iostream&gt;<br \/>\nvoid main() {<br \/>\n    long x;<br \/>\n    cin &gt;&gt; x;<br \/>\n    cout &lt;&lt; factorial(4)&lt;&lt; endl:<br \/>\n}  <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e3b\u7a0b\u5e8f\u901a\u8fc7 factorial(4)\u8fd9\u4e2a\u8bed\u53e5\u5411\u9636\u4e58\u51fd\u6570facrotial()\u7684\u5f62\u53c2n\u63d0\u4f9b\u4e86\u5b9e\u53c24\u3002\u901a\u8fc7\u8c03\u7528\u5efa\u7acb\u9636\u4e58\u51fd\u6570factorial()\u7684\u4e00\u4e2a\u6d3b\u52a8\u8bb0\u5f55,\u628a\u5f53\u524d\u7684\u5fc5\u8981\u4fe1\u606f,\u5305\u62ec\u8fd4\u56de\u5730\u5740\u3001\u53c2\u6570(\u6b64\u65f6\u4f20\u4eba4)\u3001\u5c40\u90e8\u53d8\u91cf\u7b49\u5b58\u4eba\u6808\u4e2d,\u5982\u56fe3.8(a)\u6240\u793a\u3002\u5728\u8ba1\u7b97 factorial(4)\u65f6,\u8c03\u7528\u4e86factorial(3),\u6b64\u65f6\u9700\u8981\u4e3a\u65b0\u7684\u88ab\u8c03\u51fd\u6570\u5efa\u7acb\u6d3b\u52a8\u8bb0\u5f55(\u6b64\u65f6\u4f20\u4eba\u7684\u53c2\u6570\u4e3a3)\u5e76\u538b\u4eba\u6808\u4e2d,\u6210\u4e3a\u65b0\u7684\u6808\u9876,\u4f9d\u6b21\u7c7b\u63a8factorial(3)\u8c03\u7528\u53c8\u5f15\u8d77 factorial(2)\u7684\u8c03\u7528,\u6808\u9876\u518d\u6b21\u66f4\u65b0,\u4f9d\u6b21\u8c03\u7528\u76f4\u5230\u6700\u7ec8\u8c03\u7528 factorial(0)\u6b64\u65f6 factorial(0)\u7684\u6d3b\u52a8\u8bb0\u5f55\u6210\u4e3a\u65b0\u7684\u6808\u9876\u3002\u7531\u4e8efactorial(0)\u6ee1\u8db3\u9012\u5f52\u7684\u51fa\u53e3\u6761\u4ef6,\u53ef\u4ee5\u76f4\u63a5\u5f97\u5230\u7ed3\u679c,\u6267\u884c\u7ed3\u675f\u540e,\u5176\u6d3b\u52a8\u8bb0\u5f55\u4ece\u6808\u9876\u5f39\u51fa,\u5e76\u5c06\u8ba1\u7b97\u7ed3\u679c\u548c\u63a7\u5236\u6743\u8fd4\u56de\u7ed9\u5176\u8c03\u7528\u65b9factoria(1)\u3002factorial(1)\u6839\u636efactorial(0)\u7684\u8fd4\u56de\u7ed3\u679c1\u53ef\u4ee5\u8ba1\u7b97\u51fa1!&#061;1,\u6267\u884c\u7ed3\u675f\u540e,\u4e5f\u4ece\u6808\u9876\u5f39\u51fa\u5176\u6d3b\u52a8\u8bb0\u5f55,\u7ee7\u7eed\u5c06\u63a7\u5236\u6743\u8f6c\u79fb\u7ed9\u5b83\u7684\u8c03\u7528\u65b9factorial(2),\u5982\u56fe3.8(b)\u6240\u793a,\u6309\u8fdb\u6808\u987a\u5e8f\u7684\u53cd\u5e8f\u4f9d\u6b21\u4ece\u6808\u4e2d\u5220\u9664\u6bcf\u4e2a\u6d3b\u52a8\u8bb0\u5f55,\u628a\u8ba1\u7b97\u7ed3\u679c\u548c\u63a7\u5236\u6743\u9010\u5c42\u4e0a\u79fb,\u6700\u540efactorial(4)\u628a\u63a7\u5236\u8fde\u540c\u8ba1\u7b97\u7ed3\u679c 24\u8fd4\u56de\u7ed9\u8c03\u7528\u5b83\u7684main()\u51fd\u6570\u3002\u8fd9\u6837,\u5f53\u5728main()\u4e2d\u6267\u884ccout\u8bed\u53e5\u65f6,\u53ea\u5728\u8fd0\u884c\u65f6\u73af\u5883\u4e2d\u4fdd\u7559\u4e86main()\u548c\u5168\u5c40\/\u9759\u6001\u533a\u57df\u7684\u6d3b\u52a8\u8bb0\u5f55\u3002<\/p>\n<h3 id=\"1.2.3%20%E9%80%92%E5%BD%92%E7%AE%97%E6%B3%95%E5%88%B0%E9%9D%9E%E9%80%92%E5%BD%92%E7%9A%84%E8%BD%AC%E6%8D%A2\" style=\"background-color:transparent\">1.2.3 \u9012\u5f52\u7b97\u6cd5\u5230\u975e\u9012\u5f52\u7684\u8f6c\u6362<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u9012\u5f52\u7684\u7b97\u6cd5\u5177\u6709\u53ef\u8bfb\u6027\u5f3a\u3001\u7ed3\u6784\u7b80\u7ec3\u3001\u6b63\u786e\u6027\u6613\u8bc1\u660e\u7b49\u4f18\u70b9,\u4f46\u662f\u5728\u65f6\u7a7a\u7684\u5f00\u9500\u4e0a\u76f8\u5bf9\u8f83\u5927\u3002\u4e3a\u63d0\u9ad8\u7b97\u6cd5\u7684\u65f6\u7a7a\u6548\u7387,\u5c24\u5176\u662f\u5728\u67d0\u4e9b\u5bf9\u54cd\u5e94\u65f6\u95f4\u5f88\u654f\u611f\u7684\u5b9e\u65f6\u5e94\u7528\u73af\u5883\u4e0b,\u6216\u5728\u4e0d\u652f\u6301\u9012\u5f52\u7684\u7a0b\u5e8f\u73af\u5883\u4e2d,\u5fc5\u987b\u5c06\u9012\u5f52\u7b97\u6cd5\u8f6c\u5316\u4e3a\u975e\u9012\u5f52\u7b97\u6cd5,\u95ee\u9898\u624d\u80fd\u5f97\u5230\u6709\u6548\u89e3\u51b3\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u628a\u4e00\u4e2a\u9012\u5f52\u7b97\u6cd5\u8f6c\u5316\u4e3a\u76f8\u5e94\u7684\u975e\u9012\u5f52\u7b97\u6cd5\u7684\u65b9\u6cd5\u5f88\u591a\u3002\u672c\u8282\u91cd\u70b9\u4ecb\u7ecd\u4e00\u79cd\u5229\u7528\u6808\u8fdb\u884c\u8f6c\u6362\u7684\u65b9\u6cd5,\u4ee5\u8fdb\u4e00\u6b65\u63ed\u793a\u9012\u5f52\u7684\u672c\u8d28\u4ee5\u53ca\u6808\u4e0e\u9012\u5f52\u7684\u5185\u5728\u8054\u7cfb\u00a0\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"892\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014851-682fd403370d9.png\" width=\"1272\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u9012\u5f52\u6709\u5f88\u591a\u5206\u7c7b\u65b9\u6cd5\u3002\u4f8b\u5982,\u6839\u636e\u9012\u5f52\u6240\u5904\u7684\u4f4d\u7f6e\u53ef\u5206\u4e3a\u5c3e\u90e8\u9012\u5f52\u548c\u975e\u5c3e\u90e8\u9012\u5f52,\u6240\u8c13\u5c3e\u90e8\u9012\u5f52\u662f\u6307\u9012\u5f52\u51fd\u6570\u4e2d\u6700\u540e\u4e00\u4e2a\u64cd\u4f5c\u662f\u4e00\u4e2a\u9012\u5f52\u8c03\u7528,\u4e4b\u540e\u4e0d\u518d\u6709\u5176\u4ed6\u8bed\u53e5;\u4e0e\u5c3e\u90e8\u9012\u5f52\u76f8\u5bf9\u7684\u662f\u975e\u5c3e\u90e8\u9012\u5f52\u3002\u6b64\u5916,\u6839\u636e\u9012\u5f52\u7684\u8c03\u7528\u65b9\u5f0f\u53ef\u5206\u4e3a\u76f4\u63a5\u9012\u5f52\u548c\u95f4\u63a5\u9012\u5f52,\u6839\u636e\u6709\u65e0\u5d4c\u5957\u8fd8\u53ef\u5206\u4e3a\u5d4c\u5957\u9012\u5f52\u548c\u65e0\u5d4c\u5957\u9012\u5f52\u7b49\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6839\u636e\u662f\u5426\u9700\u8981\u56de\u6eaf,\u8fd8\u53ef\u4ee5\u628a\u9012\u5f52\u5206\u6210\u7b80\u5355\u9012\u5f52\u548c\u590d\u6742\u9012\u5f52\u4e24\u79cd\u3002\u7b80\u5355\u9012\u5f52\u4e00\u822c\u53ef\u4ee5\u6839\u636e\u9012\u5f52\u5f0f\u6765\u627e\u51fa\u5176\u9012\u63a8\u516c\u5f0f\u3002\u4f8b\u5982,\u9636\u4e58\u51fd\u6570\u8fd9\u6837\u7b80\u5355\u7684\u9012\u5f52\u57fa\u672c\u4e0a\u53ef\u4ee5\u7528\u5faa\u73af\u8fed\u4ee3\u7684\u65b9\u5f0f\u6765\u53d6\u4ee3\u9012\u5f52,\u5faa\u73af\u7ed3\u675f\u6761\u4ef6\u901a\u5e38\u6bd4\u8f83\u5bb9\u6613\u786e\u5b9a\u3002\u9636\u4e58\u7684\u9009\u4ee3\u5b9e\u73b0\u5982\u7b97\u6cd51.8\u6240\u793a\u3002<\/p>\n<p>\u3010\u7b97\u6cd51.8\u3011\u9636\u4e58\u7684\u8fed\u4ee3\u5b9e\u73b0<\/p>\n<p>long factorial(long n) {<br \/>\n    long m &#061; 1;<br \/>\n    long i;<br \/>\n    if(n &gt; 0)<br \/>\n        for(i &#061; l; i &lt;&#061; n; &#043;&#043;)<br \/>\n            m &#061; m * i;<br \/>\n    return m;<br \/>\n}<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u800c\u590d\u6742\u9012\u5f52\u7684\u5faa\u73af\u7ed3\u675f\u6761\u4ef6\u5c31\u4e0d\u90a3\u4e48\u5bb9\u6613\u786e\u5b9a,\u9700\u8981\u5bf9\u6574\u4e2a\u9012\u5f52\u7a0b\u5e8f\u8fdb\u884c\u5206\u6790\u3002\u4e00\u822c\u60c5\u51b5\u4e0b,\u53ef\u6a21\u62df\u7f16\u8bd1\u7cfb\u7edf\u5904\u7406\u9012\u5f52\u7684\u673a\u5236,\u4f7f\u7528\u6808\u7b49\u6570\u636e\u7ed3\u6784\u4fdd\u5b58\u56de\u70b9\u6765\u6c42\u89e3\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4ee5\u9636\u4e58\u51fd\u6570\u4e3a\u4f8b,\u5176\u9012\u5f52\u51fa\u53e3\u4e3an&lt;&#061;0\u65f6\u8fd4\u56de\u7ed3\u679c1,\u5728n&gt;0\u7684\u60c5\u51b5\u4e0b\u5747\u9700\u6309\u7167\u4e00\u4e2a\u9012\u5f52\u89c4\u5219\u6765\u7edf\u4e00\u5904\u7406:\u6c42\u5176\u524d\u9a71n-1\u7684\u9636\u4e58\u3002\u56e0\u6b64,\u5728\u5229\u7528\u6808\u5c06\u5176\u8f6c\u6362\u6210\u975e\u9012\u5f52\u7684\u8fc7\u7a0b\u4e2d,\u9047\u5230\u4e0d\u7b49\u4e8e0\u7684\u53c2\u6570n,\u5219\u6309\u9012\u5f52\u89c4\u5219\u5c06\u5176\u538b\u6808,\u5e76\u5c06\u5176\u503c\u51cf1;\u5f53\u9047\u52300\u65f6\u5219\u505c\u6b62\u9012\u5f52,\u5c06\u5176\u7ed3\u679c\u8fd4\u56de\u7ed9\u4e0a\u5c42\u8c03\u7528\u51fd\u6570\u3002\u7b97\u6cd53.9\u662f\u6839\u636e\u6b64\u8f6c\u6362\u8fc7\u7a0b\u5f97\u5230\u7684\u975e\u9012\u5f52\u7b97\u6cd5\u7684\u6a21\u62df\u5b9e\u73b0,\u5176\u4e2d,\u7531\u4e8e\u9012\u5f52\u51fa\u53e3\u7684\u8fd4\u56de\u503c\u4e3a1,\u56e0\u6b64\u5c06\u4e2d\u95f4\u53d8\u91cfm\u7684\u521d\u503c\u7f6e\u4e3a1,\u4ee5\u7b80\u5316\u51fa\u53e3\u7684\u5904\u7406\u3002\u00a0<\/p>\n<p>\u3010\u7b97\u6cd51.9\u3011\u9636\u4e58\u7684\u4e00\u79cd\u975e\u9012\u5f52\u5b9e\u73b0<\/p>\n<p>long factorial(long n) {<br \/>\n    Stack &lt;long&gt; s;<br \/>\n    long tmp;<br \/>\n    long m &#061; l;<br \/>\n    while(n &gt; 0)            \/\/\u4e0d\u6ee1\u8db3\u9012\u5f52\u51fa\u53e3<br \/>\n        s.push(n&#8211;);        \/\/\u6309\u9012\u5f52\u89c4\u5219\u628a\u76f8\u5e94\u6570\u636e\u538b\u6808<br \/>\n    while(s.pop(&amp;tmp))      \/\/\u6ee1\u8db3\u9012\u5f52\u51fa\u53e3,\u5f00\u59cb\u8fdb\u884c\u8fd4\u56de\u5904\u7406<br \/>\n        m *&#061; tmp;<br \/>\n    return m;<br \/>\n}<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u901a\u8fc7\u4e0a\u9762\u4ecb\u7ecd\u7684\u9012\u5f52\u51fd\u6570\u7684\u5b9e\u73b0\u673a\u5236,\u53ef\u4ee5\u770b\u51fa\u7cfb\u7edf\u5904\u7406\u9012\u5f52\u7684\u7b80\u5355\u51c6\u5219:\u5f53\u9047\u5230\u9012\u5f52\u89c4\u5219(\u6d89\u53ca\u9012\u5f52\u8c03\u7528)\u65f6\u8fdb\u884c\u538b\u6808\u64cd\u4f5c,\u628a\u9012\u5f52\u51fd\u6570\u7684\u76f8\u5173\u4fe1\u606f\u4fdd\u5b58\u5728\u6808\u4e2d;\u5f53\u9047\u5230\u9012\u5f52\u51fa\u53e3\u65f6\u5219\u8fdb\u884c\u51fa\u6808\u7684\u64cd\u4f5c,\u628a\u7ed3\u675f\u4e86\u7684\u88ab\u8c03\u9012\u5f52\u51fd\u6570\u7684\u7ed3\u679c\u7b49\u4fe1\u606f\u8fd4\u56de\u4e0a\u4e00\u7ea7\u7684\u8c03\u7528\u51fd\u6570\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e0b\u9762\u4ecb\u7ecd\u6a21\u62df\u7f16\u8bd1\u7cfb\u7edf\u7684\u9012\u5f52\u673a\u5236,\u673a\u68b0\u5730\u628a\u4e00\u4e2a\u5305\u542bt\u4e2a\u9012\u5f52\u89c4\u5219\u7684\u590d\u6742\u9012\u5f52\u7b97\u6cd5\u8f6c\u6362\u4e3a\u975e\u9012\u5f52\u7b97\u6cd5\u7684\u8fc7\u7a0b\u3002\u9996\u5148\u8bbe\u7f6e\u4e00\u4e2a\u5de5\u4f5c\u6808,\u6bcf\u9047\u5230\u4e00\u4e2a\u9012\u5f52\u89c4\u5219\u90fd\u8fdb\u884c\u538b\u6808\u64cd\u4f5c,\u9047\u5230\u9012\u5f52\u51fa\u53e3\u5219\u8fdb\u884c\u51fa\u6808\u64cd\u4f5c,\u5e76\u6839\u636e\u5176\u5bf9\u5e94\u7684\u9012\u5f52\u89c4\u5219\u6765\u8fdb\u884c\u76f8\u5e94\u7684\u9012\u5f52\u8fd4\u56de\u5904\u7406\u3002\u5171\u6709t\u79cd\u4e0d\u540c\u7684\u8fd4\u56de\u5904\u7406\u60c5\u51b5,\u5916\u52a0\u6574\u4e2a\u9012\u5f52\u7a0b\u5e8f\u7684\u5165\u53e3\u548c\u51fa\u53e3\u5904\u7406,\u603b\u5171t&#043;2\u79cd\u4e0d\u540c\u7684\u5904\u7406\u60c5\u51b5\u3002\u5177\u4f53\u7684\u6a21\u62df\u8fc7\u7a0b\u53ef\u5206\u6210\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"272\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014851-682fd4039e2c2.png\" width=\"1276\" \/> <img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1564\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014851-682fd403c8f7d.png\" width=\"1262\" \/> <img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"80\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014852-682fd404c5edc.png\" width=\"1267\" \/><\/p>\n<p>\u3010\u7b97\u6cd51.10\u3011\u80cc\u5305\u95ee\u9898\u7684\u9012\u5f52\u7b97\u6cd5<\/p>\n<p>bool knap(int s,int n) {<br \/>\n    if(s &#061;&#061; 0)<br \/>\n        return true;<br \/>\n    if((s &lt; 0) || (s &gt; 0 &amp;&amp; n &lt; 1))<br \/>\n        return false;<br \/>\n    if(knap(s &#8211; w[n-1], n &#8211; 1)) {<br \/>\n        cout &lt;&lt; w[n-1]<br \/>\n        return true;<br \/>\n    }<br \/>\n    else return knap(s, n-1);<br \/>\n}<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53ef\u4ee5\u770b\u5230,\u7b97\u6cd5 1.10\u4e2d\u9012\u5f52\u7684\u51fa\u53e3\u6709\u4e24\u4e2a:\u5176\u4e00,\u5f53\u80cc\u5305\u7684\u627f\u91cd\u91cf\u4e3a0\u65f6,\u80cc\u5305\u95ee\u9898\u6709\u89e3,\u53ea\u8981\u4e0d\u9009\u62e9\u4efb\u4f55\u7269\u54c1\u5373\u53ef;\u5176\u4e8c,\u5f53\u80cc\u5305\u627f\u91cd\u91cf\u4e3a\u8d1f,\u6216\u8005\u867d\u7136\u80cc\u5305\u627f\u91cd\u91cf\u5927\u4e8e0\u4f46\u7269\u54c1\u4e2a\u6570\u5c0f\u4e8e1\u65f6,\u80cc\u5305\u95ee\u9898\u65e0\u89e3\u3002\u5f53\u6ee1\u8db3\u8fd9\u4e24\u4e2a\u51fa\u53e3\u4e4b\u4e00\u7684\u6761\u4ef6\u65f6,\u80cc\u5305\u95ee\u9898\u4fbf\u7ed3\u675f\u9012\u5f52\u3002\u5c1a\u672a\u8fbe\u5230\u9012\u5f52\u51fa\u53e3\u65f6,\u80cc\u5305\u95ee\u9898\u7684\u9012\u5f52\u89c4\u5219\u4e5f\u6709\u4e24\u79cd:<\/p>\n<ul>\n<li>\u89c4\u52191:\u82e5w[n-1]\u5305\u542b\u5728\u89e3\u4e2d,\u6c42\u89e3 knap(s-w[n-1],n-1)\u3002<\/li>\n<li>\u89c4\u52192:\u82e5w[n-1]\u4e0d\u5305\u542b\u5728\u89e3\u4e2d,\u6c42\u89e3knap(s,n-1)\u3002<\/li>\n<\/ul>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u9996\u5148\u4ece\u8bbe\u8ba1\u6808\u5f00\u59cb,\u6808\u4e2d\u6bcf\u4e2a\u7ed3\u70b9\u5e94\u8be5\u5305\u542b\u4ee5\u4e0b4\u4e2a\u57df:\u53c2\u6570s\u548cn,\u8fd4\u56de\u5730\u5740rd\u548c\u7ed3\u679c\u5355\u5143k\u3002\u7531\u4e8eknap\u7b97\u6cd5\u4e2d\u6709\u4e24\u5904\u9012\u5f52\u89c4\u5219,\u56e0\u6b64\u52a0\u4e0a\u9012\u5f52\u51fa\u53e3\u8fd4\u56de\u5730\u5740\u5171\u67093\u79cd\u60c5\u51b5:<\/p>\n<ul>\n<li>(1)\u8ba1\u7b97knap(s,n)\u5b8c\u6bd5\u8fd4\u56de\u5230\u8c03\u7528\u672c\u51fd\u6570\u7684\u5176\u4ed6\u51fd\u6570\u3002<\/li>\n<li>(2)\u8ba1\u7b97knap(s-w[n-1],n-1)\u5b8c\u6bd5,\u8fd4\u56de\u5230\u672c\u8c03\u7528\u51fd\u6570\u7ee7\u7eed\u8ba1\u7b97<\/li>\n<li>(3)\u8ba1\u7b97knap(s,n-1)\u5b8c\u6bd5,\u8fd4\u56de\u5230\u672c\u8c03\u7528\u51fd\u6570\u7ee7\u7eed\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e3a\u533a\u5206\u8fd93\u79cd\u8fd4\u56de\u60c5\u51b5,rd\u5206\u522b\u75280\u30011\u30012\u8868\u793a\u3002\u56e0\u6b64,\u6808\u7ed3\u70b9\u53ef\u58f0\u660e\u5982\u4e0b:\u00a0<\/p>\n<p>enum rdType {0, 1, 2};<br \/>\npublic class knapNode {<br \/>\n    int       s, n;        \/\/\u80cc\u5305\u7684\u627f\u91cd\u91cf\u548c\u7269\u54c1\u7684\u6570\u76ee<br \/>\n    rdType    rd;          \/\/\u8fd4\u56de\u5730\u5740<br \/>\n    bool      k;           \/\/\u7ed3\u679c\u5355\u5143<br \/>\n}<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u5e76\u5f15\u4eba\u4e24\u4e2a\u4e0e\u6808\u4e2d\u7ed3\u70b9\u7c7b\u578b\u76f8\u540c\u7684\u53d8\u91cftmp\u548cx\u4f5c\u4e3a\u8fdb\u51fa\u6808\u7684\u7f13\u51b2<\/p>\n<p>knapNode tmp, x; <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5b9a\u4e49\u4e00\u4e2a\u6808\u53d8\u91cf:<\/p>\n<p>Stack&lt;knapNode&gt; stack; <\/p>\n<p>\u3010\u7b97\u6cd51.11\u3011\u80cc\u5305\u95ee\u9898\u7684\u975e\u9012\u5f52\u5b9e\u73b0<\/p>\n<p>bool nonRecKnap(int s, int n) {<br \/>\n    tmp.s &#061; s, tmp.n &#061; n, tmp.rd &#061; 0;    \/\/\u975e\u9012\u5f52\u8c03\u7528\u5165\u53e3<br \/>\n    stack.push(tmp);<br \/>\nlabel0:                                  \/\/\u9012\u5f52\u8c03\u7528\u4eba\u53e3<br \/>\n    stack.pop(&amp;tmp);                     \/\/\u67e5\u770b\u6808\u9876\u5143\u7d20\u5e76\u5206\u60c5\u51b5\u5904\u7406<br \/>\n    if(tmp.s &#061;&#061; 0) {                     \/\/\u82e5\u6ee1\u8db3\u9012\u5f52\u51fa\u53e3\u6761\u4ef6<br \/>\n        tmp.k &#061; true;                    \/\/\u4fee\u6539\u6808\u9876\u7684\u7ed3\u679c\u5355\u5143k<br \/>\n        stack.push(tmp);<br \/>\n        goto label3;                     \/\/\u8f6c\u5411\u9012\u5f52\u51fa\u53e3\u5904\u7406<br \/>\n    }<br \/>\n    if(tmp.s &lt; 0) || (tmp.s &gt; 0 &amp;&amp; tmp.n &lt; 1) {<br \/>\n        tmp.k &#061; false;                   \/\/\u4fee\u6539\u6808\u9876\u7684\u7ed3\u679c\u5355\u5143k<br \/>\n        stack.push(tmp);<br \/>\n        goto label3;<br \/>\n    }<br \/>\n    stack.push( tmp);                    \/\/\u5c1a\u672a\u6ee1\u8db3\u9012\u5f52\u51fa\u53e3<br \/>\n    x.s&#061;tmp.s &#8211; w[tmp.n-1];              \/\/\u6309\u7167\u89c4\u52191\u8fdb\u884c\u538b\u6808\u5904\u7406<br \/>\n    x.n&#061;tmp.n &#8211; l;<br \/>\n    x.rd &#061; 1;<br \/>\n    stack.push(x);<br \/>\n    goto label0;<br \/>\nlabel1:                                  \/\/\u89c4\u52191\u5bf9\u5e94\u7684\u8fd4\u56de\u5904\u7406<br \/>\n    stack.pop(&amp;x);                       \/\/\u67e5\u770b\u6808\u9876,\u6839\u636e\u5176\u5185\u5bb9\u5206\u60c5\u51b5\u5904\u7406<br \/>\n    if(tmp.k &#061;&#061; true) {                  \/\/\u82e5\u67d0\u5c42\u7684\u7ed3\u679c\u5355\u5143\u4e3atrue<br \/>\n        x.k &#061; true;                      \/\/\u628atrue\u7ed3\u679c\u4e0a\u4f20\u7ed9\u8c03\u7528\u65b9<br \/>\n        stack.push(x);<br \/>\n        cout &lt;&lt; w[x.n-1] &lt;&lt; endl;        \/\/\u5e76\u8f93\u51fa\u5bf9\u5e94\u7684\u7269\u54c1<br \/>\n        goto label3;<br \/>\n    }<br \/>\n    stack.push(x);                       \/\/\u82e5\u67d0\u5c42\u7684\u7ed3\u679c\u5355\u5143\u4e3afalse<br \/>\n    tmp.s &#061; x.s;                         \/\/\u5f53\u524d\u7269\u54c1\u7684\u9009\u62e9\u4e0d\u5408\u9002,\u56de\u6eaf,\u8c03\u7528\u89c4\u52192<br \/>\n    tmp.n &#061; x.n &#8211; 1;                     \/\/\u6309\u7167\u89c4\u52192\u8fdb\u884c\u538b\u6808\u5904\u7406<br \/>\n    tmp.rd &#061; 2;<br \/>\n    stack.push(tmp);<br \/>\n    goto label0;<br \/>\nlabel2:                                  \/\/\u89c4\u52192\u5bf9\u5e94\u7684\u8fd4\u56de\u5904\u7406<br \/>\n    stack.pop(&amp;x);<br \/>\n    x.k&#061; tmp.k;                          \/\/\u7ed3\u679c\u5355\u5143k\u7684\u5185\u5bb9\u4e0a\u4f20\u7ed9\u8c03\u7528\u65b9<br \/>\n    stack.push(x);<br \/>\nlabel3:                                  \/\/\u9012\u5f52\u51fa\u53e3\u5904\u7406<br \/>\n    stack.pop(&amp;tmp);<br \/>\n    switch(tmp.rd) {<br \/>\n        case 0: return tmp.k;            \/\/\u7b97\u6cd5\u7ed3\u675f\u5e76\u8fd4\u56de\u7ed3\u679c<br \/>\n        case l: goto labell;             \/\/\u8f6c\u5411\u89c4\u52191\u7684\u8fd4\u56de\u5904\u7406\u5904<br \/>\n        case 2: goto label2;             \/\/\u8f6c\u5411\u89c4\u52192\u7684\u8fd4\u56de\u5904\u7406\u5904<br \/>\n    }<br \/>\n} <\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u800c\u540e,\u8f6c\u6362\u6309\u4e0b\u9762\u65b9\u5f0f\u8fdb\u884c:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"554\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014852-682fd404df09a.png\" width=\"1278\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8fd9\u6837\u8f6c\u6362\u4e4b\u540e\u4fbf\u53ef\u5f97\u5230\u4e00\u4e2a\u5982\u7b97\u6cd51.11\u6240\u793a\u7684\u529f\u80fd\u76f8\u540c\u7684\u975e\u9012\u5f52\u7b97\u6cd5\u3002\u8be5\u7b97\u6cd5\u6267\u884c\u7ed3\u675f\u65f6,\u80cc\u5305\u95ee\u9898\u7684\u89e3\u5728tmp.k\u4e2d,\u82e5\u5176\u503c\u4e3atue,\u5219\u5177\u4f53\u9009\u4e2d\u7684 w[i]\u5df2\u5728\u7b97\u6cd5\u4e2d\u8f93\u51fa\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53ef\u4ee5\u770b\u51fa,\u7b97\u6cd51.11\u4e2d\u6709\u591a\u5904\u5f39\u51fa\u6808\u9876\u5143\u7d20\u3001\u4fee\u6539\u5176\u4e2d\u7684\u7ed3\u679c\u5355\u5143k\u540e\u53c8\u91cd\u65b0\u538b\u6808\u7684\u64cd\u4f5c\u3002\u4ed4\u7ec6\u5206\u6790\u8fd9\u4e9b\u64cd\u4f5c\u4e4b\u540e,\u4f1a\u53d1\u73b0\u7b97\u6cd51.11\u8fd8\u6709\u8fdb\u4e00\u6b65\u4f18\u5316\u7684\u4f59\u5730\u3002\u9996\u5148,\u4ece\u8ba1\u7b97\u8fc7\u7a0b\u6765\u770b,\u4e00\u65e6\u5728\u67d0\u5c42\u8c03\u7528\u4e2d\u76f8\u5e94\u6808\u9876\u5143\u7d20\u7684k\u503c\u4e3atrue,\u5219\u6b64\u80cc\u5305\u95ee\u9898\u7684\u89e3\u4e3a\u771f,\u5e76\u4e14\u5c06trnue \u9010\u5c42\u5411\u5916\u4f20\u9012,\u5c06\u5404\u5c42\u7684k\u90fd\u7f6e\u6210true,\u56e0\u6b64\u53ef\u91c7\u7528\u4e00\u4e2a\u5168\u7a0b\u53d8\u91cfk\u4f5c\u4e3a\u7ed3\u679c\u5355\u5143,\u5c06\u5176\u521d\u59cb\u72b6\u6001\u8bbe\u7f6e\u4e3afalse\u3002\u4e00\u65e6\u7f6e\u6210true\u540e\u5c31\u4e0d\u518d\u53d8\u5316,\u4ece\u800c\u53ef\u7701\u53bb\u6808\u4e2d\u5404\u7ed3\u70b9\u7684k\u6570\u636e\u57df\u53ca\u5176\u591a\u6b21\u7684\u91cd\u590d\u8d4b\u503c\u548c\u8fdb\u6808\u3001\u51fa\u6808\u64cd\u4f5c\u3002\u5176\u6b21,\u4ece\u8c03\u7528\u8fc7\u7a0b\u6765\u770b,\u6bcf\u9012\u5f52\u8fdb\u4eba\u4e00\u5c42,\u53c2\u6570n\u5c31\u51cf1,\u6808\u7684\u6307\u9488 top \u5c31\u52a01,\u53ea\u8981\u5728\u8ba1\u7b97\u524d\u5c06 top \u7f6e\u4e3a -1,\u5219\u9012\u5f52\u8fc7\u7a0b\u603b\u6709\u6808\u9876\u5143\u7d20\u7684n\u6570\u636e\u57df\u6ee1\u8db3 st[top].n&#043;t&#061;no,st[top].n&lt;l\u7684\u6761\u4ef6\u5c31\u7b49\u4ef7\u4e8et&gt;n\u3002-1,\u5982\u679c\u6808\u9876\u6307\u9488\u53ef\u8bbf\u95ee,\u5c31\u53ef\u7701\u53bb\u6808\u4e2d\u7684\u6570\u636e\u57dfn,\u7528t&gt;n\u3002-1\u66ff\u6362\u7b97\u6cd5\u4e2dtmp.n&lt;1:\u6700\u540e,\u6d88\u9664\u7b97\u6cd5\u4e2d\u7684goto\u8bed\u53e5,\u4fbf\u5f97\u5230\u66f4\u4e3a\u7b80\u6d01\u4f18\u5316\u7684\u7b97\u6cd51.12\u3002\u6539\u8fdb\u540e\u7684\u7248\u672c\u00a0\u4e2d\u6808\u7684\u7ed3\u70b9\u7c7b\u578b\u66ff\u6362\u6210\u4e0b\u8ff0\u7684\u5f62\u5f0f:<\/p>\n<p>public class knapNode {<br \/>\n    int       s;        \/\/\u80cc\u5305\u7684\u627f\u91cd\u91cf<br \/>\n    rdType    rd;       \/\/\u8fd4\u56de\u5730\u5740<br \/>\n} <\/p>\n<p>\u3010\u7b97\u6cd51.12\u3011\u80cc\u5305\u95ee\u9898\u7684\u975e\u9012\u5f52\u5b9e\u73b0\u7684\u4f18\u5316\u7248<\/p>\n<p>bool nonRecKnapOpt(int s, int n) {<br \/>\n    int t,n0 &#061; n;<br \/>\n    bool k &#061; false;<\/p>\n<p>    tmp.s &#061; s, tmp.rd &#061; 0;<br \/>\n    stack.push(tmp);<\/p>\n<p>    while(!stack.isEmpty()) {<br \/>\n        t &#061; stack.top;<br \/>\n        stack.top(&amp;tmp);<\/p>\n<p>        while(tmp.s &gt;&#061; 0) &amp;&amp; (tmp.s &lt;&#061; 0 || n0 &gt; t) {    \/\/\u5904\u7406\u6808\u9876\u5143\u7d20,\u4ee5\u5224\u65ad\u662f\u5426\u6ee1\u8db3<br \/>\n                                                         \/\/\u9012\u5f52\u51fa\u53e3\u6761\u4ef6<br \/>\n        if(tmp.s &#061;&#061; 0) {<br \/>\n            k &#061; true;<br \/>\n            break;<br \/>\n        } else {                                         \/\/\u5c1a\u672a\u8fbe\u9012\u5f52\u51fa\u53e3\u524d,\u6309\u89c4\u52191\u8fdb\u884c\u538b\u6808\u64cd\u4f5c<br \/>\n            x.s &#061; tmp.s &#8211; w[n0 &#8211; 1 &#8211; t];<br \/>\n            x.rd &#061; 1;<br \/>\n            stack,push(x);<br \/>\n        }<br \/>\n        t &#061; stack.top;<br \/>\n        stack.top(&amp;tmp);<br \/>\n        }<br \/>\n        while(!stack.isEmpty()) {                        \/\/\u8fd4\u56de\u5904\u7406<br \/>\n            stack.pop(&amp;tmp);<br \/>\n            t &#061; stack.top;<br \/>\n            if(tmp.rd &#061;&#061; 0)                              \/\/\u7b97\u6cd5\u7ed3\u675f<br \/>\n                return k;<br \/>\n            if(tmp.rd &#061;&#061; 1)                              \/\/\u4ece\u89c4\u52191\u8fd4\u56de<br \/>\n                if(k &#061;&#061; true)                            \/\/\u7ed3\u679c\u4e3a\u771f\u5219\u6253\u5370\u5bf9\u5e94\u7684\u7269\u54c1<br \/>\n                    cout &lt;&lt; w[n0 &#8211; 1 &#8211; t] &lt;&lt;endl;<br \/>\n                else {                                   \/\/\u5426\u5219\u56de\u6eaf,\u91c7\u7528\u89c4\u52192\u8fdb\u6808<br \/>\n                    stack.top(&amp;x);<br \/>\n                    tmp.s &#061; x.s;<br \/>\n                    tmp.rd &#061; 2;<br \/>\n                    stack.push(tmp);<br \/>\n                    break;<br \/>\n                }<br \/>\n        }<br \/>\n    }<br \/>\n} <\/p>\n<h2 id=\"1.3%C2%A0%E6%A0%88%E7%9A%84%E6%B7%B1%E5%85%A5%E8%AE%A8%E8%AE%BA\">1.3\u00a0\u6808\u7684\u6df1\u5165\u8ba8\u8bba<\/h2>\n<h3 id=\"1.3.1%20%E9%A1%BA%E5%BA%8F%E6%A0%88%E4%B8%8E%E9%93%BE%E5%BC%8F%E6%A0%88%E7%9A%84%E6%AF%94%E8%BE%83\">1.3.1 \u987a\u5e8f\u6808\u4e0e\u94fe\u5f0f\u6808\u7684\u6bd4\u8f83<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6808\u7684\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb,\u53ea\u8981\u6ee1\u8db3\u540e\u8fdb\u5148\u51fa\u7684\u7279\u6027\u90fd\u53ef\u4ee5\u4f7f\u7528\u6808\u7ed3\u6784\u3002\u4f8b\u5982,\u51fd\u6570\u8c03\u7528\u548c\u9012\u5f52\u5b9e\u73b0\u3001\u6df1\u5ea6\u4f18\u5148\u5468\u6e38\u3001\u8868\u8fbe\u5f0f\u8f6c\u6362\u548c\u6c42\u503c\u7b49\u3002<\/p>\n<p>1. \u987a\u5e8f\u6808\u4e0e\u94fe\u5f0f\u6808\u7684\u6bd4\u8f83<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u987a\u5e8f\u6808\u548c\u94fe\u5f0f\u6808\u7684\u57fa\u672c\u64cd\u4f5c\u90fd\u53ea\u9700\u8981\u5e38\u6570\u65f6\u95f4,\u56e0\u6b64\u4e8c\u8005\u5728\u65f6\u95f4\u6548\u7387\u4e0a\u96be\u5206\u4f2f\u4ef2\u3002\u4ece\u7a7a\u95f4\u89d2\u5ea6\u6765\u770b,\u521d\u59cb\u65f6\u987a\u5e8f\u6808\u5fc5\u987b\u8bf4\u660e\u4e00\u4e2a\u56fa\u5b9a\u7684\u957f\u5ea6,\u5f53\u6808\u4e0d\u591f\u6ee1\u65f6,\u52bf\u5fc5\u6d6a\u8d39\u4e00\u4e9b\u7a7a\u95f4;\u94fe\u5f0f\u6808\u7684\u957f\u5ea6\u53ef\u6839\u636e\u9700\u8981\u800c\u589e\u51cf,\u4f46\u6bcf\u4e2a\u5143\u7d20\u90fd\u9700\u8981\u4e00\u4e2a\u6307\u9488\u57df,\u4ece\u800c\u4ea7\u751f\u7ed3\u6784\u6027\u5f00\u9500\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5728\u6808\u7684\u5b9e\u9645\u5e94\u7528\u4e2d,\u6709\u65f6\u9700\u8981\u8bbf\u95ee\u6808\u7684\u5185\u90e8\u5143\u7d20\u3002\u987a\u5e8f\u6808\u53ef\u4ee5\u6839\u636e\u5143\u7d20\u4e0e\u6808\u9876\u7684\u76f8\u5bf9\u4f4d\u7f6e\u5feb\u901f\u5b9a\u4f4d\u5e76\u8bfb\u53d6\u5185\u90e8\u5143\u7d20,\u800c\u94fe\u5f0f\u6808\u5219\u9700\u8981\u6cbf\u7740\u6307\u9488\u94fe\u904d\u5386\u624d\u80fd\u8bbf\u95ee\u5185\u90e8\u5143\u7d20\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u9274\u4e8e\u4e0a\u8ff0\u539f\u56e0,\u987a\u5e8f\u6808\u5728\u5b9e\u9645\u4e2d\u66f4\u4e3a\u5e38\u7528\u4e00\u4e9b\u3002<\/p>\n<p>2. \u591a\u6808\u7ba1\u7406<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5728\u7f16\u8bd1\u7cfb\u7edf\u7b49\u8ba1\u7b97\u673a\u7cfb\u7edf\u8f6f\u4ef6\u4e2d,\u5f80\u5f80\u540c\u65f6\u4f7f\u7528\u548c\u7ba1\u7406\u591a\u4e2a\u6808,\u8fd9\u65f6\u53ef\u5145\u5206\u5229\u7528\u987a\u5e8f\u6808\u5355\u5411\u5ef6\u4f38\u7684\u7279\u6027\u3002\u4f8b\u5982,\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u6570\u7ec4\u6765\u5b58\u50a8\u4e24\u4e2a\u6808,\u8ba9\u5b83\u4eec\u5171\u4eab\u540c\u4e00\u5b58\u50a8\u7a7a\u95f4\u3002\u628a\u6570\u7ec4\u7684\u4e24\u7aef\u8bbe\u7f6e\u4e3a\u4e24\u8005\u5404\u81ea\u7684\u6808\u5e95,\u4ece\u4e24\u7aef\u5f00\u59cb\u5411\u4e2d\u95f4\u8fce\u9762\u5ef6\u4f38(\u89c1\u56fe3.13),\u8fd9\u6837\u6d6a\u8d39\u7684\u7a7a\u95f4\u76f8\u5bf9\u5c11\u4e00\u4e9b\u5728\u4e24\u4e2a\u6808\u7684\u7a7a\u95f4\u9700\u6c42\u6070\u597d\u76f8\u53cd,\u5373\u4e00\u4e2a\u6808\u589e\u957f\u53e6\u4e00\u4e2a\u6536\u7f29\u65f6,\u8fd9\u79cd\u65b9\u6cd5\u5f88\u594f\u6548\u3002\u53cd\u4e4b,\u5982\u679c\u4e24\u4e2a\u6808\u540c\u65f6\u589e\u957f,\u6570\u7ec4\u4e2d\u95f4\u7684\u7a7a\u95f4\u4f1a\u5f88\u5feb\u7528\u5b8c\u3002<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"274\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014853-682fd405330b7.png\" width=\"1511\" \/><\/p>\n<h3 id=\"1.3.2%20%E9%99%90%E5%88%B6%E5%AD%98%E5%8F%96%E7%82%B9%E7%9A%84%E8%A1%A8\">1.3.2 \u9650\u5236\u5b58\u53d6\u70b9\u7684\u8868<\/h3>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u5229\u7528\u6808\u548c\u961f\u5217\u7684\u601d\u60f3\u53ef\u4ee5\u8bbe\u8ba1\u51fa\u4e00\u4e9b\u53d8\u79cd\u7684\u6808\u6216\u961f\u5217\u7ed3\u6784\u3002\u867d\u7136\u5b83\u4eec\u7684\u5e94\u7528\u6ca1\u6709\u6808\u548c\u961f\u5217\u90a3\u6837\u5e7f\u6cdb,\u4f46\u5728\u4e00\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\u5177\u6709\u5f88\u597d\u7684\u5e94\u7528\u4ef7\u503c\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(1)\u53cc\u7aef\u961f\u5217:\u9650\u5236\u63d2\u4eba\u548c\u5220\u9664\u5728\u7ebf\u6027\u8868\u7684\u4e24\u7aef\u8fdb\u884c\u3002\u6808\u548c\u961f\u5217\u90fd\u662f\u7279\u6b8a\u7684\u53cc\u7aef\u961f\u5217,\u5bf9\u5176\u5b58\u53d6\u8bbe\u7f6e\u4e86\u9650\u5236\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(2)\u53cc\u6808:\u4e24\u4e2a\u5e95\u90e8\u76f8\u8fde\u7684\u6808\u3002\u53cc\u6808\u662f\u4e00\u79cd\u6dfb\u52a0\u9650\u5236\u7684\u53cc\u7aef\u961f\u5217,\u5b83\u89c4\u5b9a\u4eceend1 \u63d2\u5165\u7684\u5143\u7d20\u53ea\u80fd\u4eceend1\u7aef\u5220\u9664,\u800c\u4eceend2 \u63d2\u4eba\u7684\u5143\u7d20\u53ea\u80fd\u4ece end2 \u7aef\u5220\u9664\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(3)\u8d85\u961f\u5217:\u4e00\u79cd\u5220\u9664\u53d7\u9650\u7684\u53cc\u7aef\u961f\u5217,\u5220\u9664\u53ea\u5141\u8bb8\u5728\u4e00\u7aef\u8fdb\u884c,\u800c\u63d2\u5165\u53ef\u5728\u4e24\u7aef\u8fdb\u884c\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0(4)\u8d85\u6808:\u4e00\u79cd\u63d2\u4eba\u53d7\u9650\u7684\u53cc\u7aef\u961f\u5217,\u63d2\u4eba\u53ea\u9650\u5236\u5728\u4e00\u7aef\u800c\u5220\u9664\u5141\u8bb8\u5728\u4e24\u7aef\u8fdb\u884c\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7531\u6b64\u53ef\u89c1,\u8fd9\u51e0\u79cd\u9650\u5236\u5b58\u53d6\u70b9\u7684\u8868\u90fd\u662f\u67d0\u79cd\u53d7\u9650\u7684\u53cc\u7aef\u961f\u5217\u3002STL\u63d0\u4f9b\u7684\u57fa\u672c\u5e8f\u5217\u4e2d\u5c31\u6709\u53cc\u7aef\u961f\u5217 deque,\u6808\u548c\u961f\u5217\u90fd\u662f\u901a\u8fc7 deque \u5b9e\u73b0\u7684\u3002\u00a0\u00a0<\/p>\n<hr \/>\n<h2 id=\"%E6%9C%AC%E7%AB%A0%E5%B0%8F%E7%BB%93\">\u672c\u7ae0\u5c0f\u7ed3<\/h2>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6808\u662f\u4e00\u79cd\u9650\u5236\u8bbf\u95ee\u7aef\u53e3\u7684\u7ebf\u6027\u8868,\u5e38\u88ab\u79f0\u4e3a\u540e\u8fdb\u5148\u51fa(LIFO)\u8868\u3002\u5176\u5143\u7d20\u7684\u63d2\u5165\u548c\u5220\u9664\u90fd\u53ea\u80fd\u5728\u8868\u7684\u4e00\u7aef\u8fdb\u884c,\u8be5\u7aef\u79f0\u4e3a\u6808\u7684\u6808\u9876,\u53e6\u4e00\u7aef\u5219\u53eb\u505a\u6808\u5e95\u3002\u6808\u7684\u7279\u70b9\u662f\u6bcf\u6b21\u53d6\u51fa(\u5e76\u88ab\u5220\u9664)\u7684\u5143\u7d20\u603b\u662f\u5f53\u524d\u6808\u5185\u5143\u7d20\u4e2d\u6700\u540e\u538b\u4eba\u7684,\u800c\u6700\u5148\u538b\u4eba\u7684\u5143\u7d20\u5219\u88ab\u653e\u5728\u6808\u7684\u5e95\u90e8,\u8981\u5230\u6700\u540e\u624d\u80fd\u53d6\u51fa\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6808\u7684\u8fd0\u7b97\u5305\u62ec\u538b\u6808\u3001\u51fa\u6808\u3001\u8fd4\u56de\u6808\u9876\u5143\u7d20\u3001\u5224\u65ad\u6808\u662f\u5426\u4e3a\u7a7a\u7b49\u8fd0\u7b97,\u8fd0\u7b97\u7684\u65f6\u95f4\u4ee3\u4ef7\u5747\u4e3a\u5e38\u6570\u65f6\u95f4\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6808\u7684\u5b9e\u73b0\u901a\u5e38\u6709\u987a\u5e8f\u6808\u548c\u94fe\u5f0f\u6808\u4e24\u79cd\u3002\u987a\u5e8f\u6808\u7528\u6570\u7ec4\u5b9e\u73b0;\u800c\u94fe\u5f0f\u6808\u5219\u7528\u5355\u94fe\u8868\u65b9\u5f0f\u5b58\u50a8\u5176\u4e2d\u7684\u6307\u9488\u65b9\u5411\u662f\u4ece\u6808\u9876\u5411\u4e0b\u94fe\u63a5\u3002\u5728\u5b9e\u9645\u4e2d,\u987a\u5e8f\u6808\u6bd4\u94fe\u5f0f\u6808\u4f7f\u7528\u5f97\u66f4\u4e3a\u5e7f\u6cdb\u3002 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6808\u662f\u4e00\u79cd\u5f88\u91cd\u8981\u3001\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u7684\u6570\u636e\u7ed3\u6784\u3002\u5e38\u89c1\u7684\u5e94\u7528\u5305\u62ec\u8868\u8fbe\u5f0f\u8f6c\u6362\u548c\u6c42\u503c\u3001\u51fd\u6570\u8c03\u7528\u548c\u9012\u5f52\u5b9e\u73b0\u3001\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b49\u3002\u6808\u7684\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u5e94\u7528\u5728\u4e8e\u5bf9\u51fd\u6570\u8c03\u7528\u673a\u5236\u548c\u9012\u5f52\u5b9e\u73b0\u7684\u652f\u6301\u672c\u7ae0\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u7684\u5b9e\u73b0\u673a\u5236\u548c\u9012\u5f52\u7b97\u6cd5\u5230\u975e\u9012\u5f52\u7b97\u6cd5\u7684\u8f6c\u6362\u63ed\u793a\u4e86\u9012\u5f52\u7684\u672c\u8d28\u4ee5\u53ca\u6808\u4e0e\u9012\u5f52\u7684\u5185\u5728\u8054\u7cfb\u3002<\/p>\n<hr \/>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u672c\u535a\u6587\u76ee\u7684\u5728\u4e8e\u603b\u7ed3\u6574\u7406\u5e76\u4ecb\u7ecd\u6808\u7684\u57fa\u672c\u77e5\u8bc6&#xff0c;\u5173\u4e8e\u6808\u7684\u5e94\u7528\u90e8\u5206&#xff0c;\u8fd9\u91cc\u4e0d\u505a\u66f4\u591a\u5c55\u5f00\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u8868\u8fbe\u5f0f\u6c42\u503c&#xff1a;\u4e66 52-57 \u00a0 \u00a0 \u00a0 \u00a0 \u62ec\u53f7\u5339\u914d&#xff1a;Java_300_Day15<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb936\u6b21\uff0c\u70b9\u8d5e13\u6b21\uff0c\u6536\u85cf27\u6b21\u3002\u4ee5\u9636\u4e58\u51fd\u6570\u4e3a\u4f8b\u6765\u8bf4\u660e\u9012\u5f52\u7684\u5b9a\u4e49\u3002\u9636\u4e58n!\u7684\u9012\u5f52\u5b9a\u4e49\u5982\u4e0b\uff1a\u4e3a\u4e86\u5b9a\u4e49\u6574\u6570n\u7684\u9636\u4e58,\u5fc5\u987b\u5148\u5b9a\u4e49(n-1)\u7684\u9636\u4e58,\u800c\u4e3a\u4e86\u5b9a\u4e49(n-1)\u7684\u9636\u4e58,\u53c8\u9700\u5148\u5b9a\u4e49(n-2)\u7684\u9636\u4e58,\u5982\u6b64\u76f4\u52300\u4e3a\u6b62,\u56e0\u4e3a\u6b64\u65f6\u9636\u4e58\u5b9a\u4e49\u4e3a1\u3002\u8fd9\u79cd\u7528\u81ea\u8eab\u7684\u7b80\u5355\u60c5\u51b5\u6765\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u5b9a\u4e49\u81ea\u5df1\u7684\u65b9\u5f0f\u79f0\u4e3a\u9012\u5f52\u5b9a\u4e49\u3002\u53ef\u4ee5\u770b\u51fa,\u4e00\u4e2a\u9012\u5f52\u5b9a\u4e49\u7531\u4e24\u90e8\u5206\u7ec4\u6210\u3002\u5176\u4e00\u4e3a\u9012\u5f52\u57fa\u7840,\u4e5f\u79f0\u9012\u5f52\u51fa\u53e3,\u662f\u9012\u5f52\u5b9a\u4e49\u7684\u6700\u57fa\u672c\u60c5\u51b5,\u4e5f\u662f\u4fdd\u8bc1\u9012\u5f52\u7ed3\u675f\u7684\u524d\u63d0;\u5176\u4e8c\u5219\u4e3a\u9012\u5f52\u89c4\u5219,\u786e\u5b9a\u4e86\u7531\u7b80\u5355\u60c5\u51b5\u6784\u7b51\u590d\u6742\u60c5\u51b5\u9700\u9075\u5faa\u7684\u89c4\u5219\u3002\u4e0a\u9762\u7684\u9636\u4e58\u5b9a\u4e49\u4e2d\u9012\u5f52\u51fa\u53e3\u5373\u4e3an\u22640,\u6b64\u65f6\u9636\u4e58\u5b9a\u4e49\u4e3a1;<\/p>\n","protected":false},"author":2,"featured_media":39264,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[1813,3509],"topic":[],"class_list":["post-39276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-1813","tag-3509"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u6570\u636e\u7ed3\u6784\u3011\u6808 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/39276.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u6570\u636e\u7ed3\u6784\u3011\u6808 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb936\u6b21\uff0c\u70b9\u8d5e13\u6b21\uff0c\u6536\u85cf27\u6b21\u3002\u4ee5\u9636\u4e58\u51fd\u6570\u4e3a\u4f8b\u6765\u8bf4\u660e\u9012\u5f52\u7684\u5b9a\u4e49\u3002\u9636\u4e58n!\u7684\u9012\u5f52\u5b9a\u4e49\u5982\u4e0b\uff1a\u4e3a\u4e86\u5b9a\u4e49\u6574\u6570n\u7684\u9636\u4e58,\u5fc5\u987b\u5148\u5b9a\u4e49(n-1)\u7684\u9636\u4e58,\u800c\u4e3a\u4e86\u5b9a\u4e49(n-1)\u7684\u9636\u4e58,\u53c8\u9700\u5148\u5b9a\u4e49(n-2)\u7684\u9636\u4e58,\u5982\u6b64\u76f4\u52300\u4e3a\u6b62,\u56e0\u4e3a\u6b64\u65f6\u9636\u4e58\u5b9a\u4e49\u4e3a1\u3002\u8fd9\u79cd\u7528\u81ea\u8eab\u7684\u7b80\u5355\u60c5\u51b5\u6765\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u5b9a\u4e49\u81ea\u5df1\u7684\u65b9\u5f0f\u79f0\u4e3a\u9012\u5f52\u5b9a\u4e49\u3002\u53ef\u4ee5\u770b\u51fa,\u4e00\u4e2a\u9012\u5f52\u5b9a\u4e49\u7531\u4e24\u90e8\u5206\u7ec4\u6210\u3002\u5176\u4e00\u4e3a\u9012\u5f52\u57fa\u7840,\u4e5f\u79f0\u9012\u5f52\u51fa\u53e3,\u662f\u9012\u5f52\u5b9a\u4e49\u7684\u6700\u57fa\u672c\u60c5\u51b5,\u4e5f\u662f\u4fdd\u8bc1\u9012\u5f52\u7ed3\u675f\u7684\u524d\u63d0;\u5176\u4e8c\u5219\u4e3a\u9012\u5f52\u89c4\u5219,\u786e\u5b9a\u4e86\u7531\u7b80\u5355\u60c5\u51b5\u6784\u7b51\u590d\u6742\u60c5\u51b5\u9700\u9075\u5faa\u7684\u89c4\u5219\u3002\u4e0a\u9762\u7684\u9636\u4e58\u5b9a\u4e49\u4e2d\u9012\u5f52\u51fa\u53e3\u5373\u4e3an\u22640,\u6b64\u65f6\u9636\u4e58\u5b9a\u4e49\u4e3a1;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/39276.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-23T01:48:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014850-682fd40231b1a.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39276.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/39276.html\",\"name\":\"\u3010\u6570\u636e\u7ed3\u6784\u3011\u6808 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-23T01:48:54+00:00\",\"dateModified\":\"2025-05-23T01:48:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/39276.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/39276.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/39276.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u6570\u636e\u7ed3\u6784\u3011\u6808\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010\u6570\u636e\u7ed3\u6784\u3011\u6808 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/39276.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u6570\u636e\u7ed3\u6784\u3011\u6808 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb936\u6b21\uff0c\u70b9\u8d5e13\u6b21\uff0c\u6536\u85cf27\u6b21\u3002\u4ee5\u9636\u4e58\u51fd\u6570\u4e3a\u4f8b\u6765\u8bf4\u660e\u9012\u5f52\u7684\u5b9a\u4e49\u3002\u9636\u4e58n!\u7684\u9012\u5f52\u5b9a\u4e49\u5982\u4e0b\uff1a\u4e3a\u4e86\u5b9a\u4e49\u6574\u6570n\u7684\u9636\u4e58,\u5fc5\u987b\u5148\u5b9a\u4e49(n-1)\u7684\u9636\u4e58,\u800c\u4e3a\u4e86\u5b9a\u4e49(n-1)\u7684\u9636\u4e58,\u53c8\u9700\u5148\u5b9a\u4e49(n-2)\u7684\u9636\u4e58,\u5982\u6b64\u76f4\u52300\u4e3a\u6b62,\u56e0\u4e3a\u6b64\u65f6\u9636\u4e58\u5b9a\u4e49\u4e3a1\u3002\u8fd9\u79cd\u7528\u81ea\u8eab\u7684\u7b80\u5355\u60c5\u51b5\u6765\u76f4\u63a5\u6216\u95f4\u63a5\u5730\u5b9a\u4e49\u81ea\u5df1\u7684\u65b9\u5f0f\u79f0\u4e3a\u9012\u5f52\u5b9a\u4e49\u3002\u53ef\u4ee5\u770b\u51fa,\u4e00\u4e2a\u9012\u5f52\u5b9a\u4e49\u7531\u4e24\u90e8\u5206\u7ec4\u6210\u3002\u5176\u4e00\u4e3a\u9012\u5f52\u57fa\u7840,\u4e5f\u79f0\u9012\u5f52\u51fa\u53e3,\u662f\u9012\u5f52\u5b9a\u4e49\u7684\u6700\u57fa\u672c\u60c5\u51b5,\u4e5f\u662f\u4fdd\u8bc1\u9012\u5f52\u7ed3\u675f\u7684\u524d\u63d0;\u5176\u4e8c\u5219\u4e3a\u9012\u5f52\u89c4\u5219,\u786e\u5b9a\u4e86\u7531\u7b80\u5355\u60c5\u51b5\u6784\u7b51\u590d\u6742\u60c5\u51b5\u9700\u9075\u5faa\u7684\u89c4\u5219\u3002\u4e0a\u9762\u7684\u9636\u4e58\u5b9a\u4e49\u4e2d\u9012\u5f52\u51fa\u53e3\u5373\u4e3an\u22640,\u6b64\u65f6\u9636\u4e58\u5b9a\u4e49\u4e3a1;","og_url":"https:\/\/www.wsisp.com\/helps\/39276.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-23T01:48:54+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250523014850-682fd40231b1a.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/39276.html","url":"https:\/\/www.wsisp.com\/helps\/39276.html","name":"\u3010\u6570\u636e\u7ed3\u6784\u3011\u6808 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-23T01:48:54+00:00","dateModified":"2025-05-23T01:48:54+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/39276.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/39276.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/39276.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u6570\u636e\u7ed3\u6784\u3011\u6808"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=39276"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/39276\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/39264"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=39276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=39276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=39276"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=39276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}