{"id":203011,"date":"2025-05-22T11:15:32","date_gmt":"2025-05-22T03:15:32","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203011\/"},"modified":"2025-05-22T11:15:32","modified_gmt":"2025-05-22T03:15:32","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e4%b8%ad%e9%80%92%e5%bd%92%e5%bc%8f%e8%b4%aa%e5%bf%83%e7%ae%97%e6%b3%95%e7%9a%84%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203011\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9012\u5f52\u5f0f\u8d2a\u5fc3\u7b97\u6cd5\u7684\u6280\u5de7"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9012\u5f52\u5f0f\u8d2a\u5fc3\u7b97\u6cd5\u7684\u6280\u5de7<\/h1>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9012\u5f52\u5f0f\u8d2a\u5fc3\u7b97\u6cd5\u7684\u6280\u5de7\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>\u9012\u5f52\u5f0f\u8d2a\u5fc3\u7b97\u6cd5\u662f\u4e00\u79cd\u51fd\u6570\u5f0f\u7f16\u7a0b\u7b56\u7565\uff0c\u7528\u4e8e\u89e3\u51b3\u4f18\u5316\u95ee\u9898\uff0c\u5b83\u7ed3\u5408\u4e86\u9012\u5f52\u548c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u4f18\u52bf\uff1a\u57fa\u7840\u6848\u4f8b\uff1a\u5f53\u95ee\u9898\u53ef\u4ee5\u8f7b\u677e\u89e3\u51b3\u65f6\u786e\u5b9a\u3002\u9012\u5f52\u8c03\u7528\uff1a\u5c06\u95ee\u9898\u5206\u89e3\u4e3a\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u5e76\u9012\u5f52\u8c03\u7528\u7b97\u6cd5\u3002\u5408\u5e76\u7ed3\u679c\uff1a\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u5408\u5e76\u4ee5\u83b7\u5f97\u539f\u59cb\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u3002\u8d2a\u5fc3\u9009\u62e9\uff1a\u5728\u6bcf\u4e2a\u9012\u5f52\u6b65\u9aa4\u4e2d\uff0c\u4ece\u53ef\u7528\u9009\u9879\u4e2d\u9009\u62e9\u5c40\u90e8\u6700\u4f73\u9009\u62e9\u3002\u5b9e\u6218\u6848\u4f8b\uff1a\u80cc\u5305\u95ee\u9898\u4e2d\uff0c\u4f7f\u7528 Java \u4ee3\u7801\uff0c\u8be5\u7b97\u6cd5\u5c06\u7269\u54c1\u7ec4\u5408\u653e\u5165\u80cc\u5305\uff0c\u4f7f\u5176\u603b\u4ef7\u503c\u6700\u5927\u5316\uff0c\u540c\u65f6\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730019224671dff98b1ac2.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9012\u5f52\u5f0f\u8d2a\u5fc3\u7b97\u6cd5\u7684\u6280\u5de7<\/strong><\/p>\n<p>\u9012\u5f52\u5f0f\u8d2a\u5fc3\u7b97\u6cd5\u662f\u4e00\u79cd\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u89e3\u51b3\u4f18\u5316\u95ee\u9898\u7684\u5f3a\u5927\u7b56\u7565\u3002\u5b83\u7ed3\u5408\u4e86\u9012\u5f52\u7684\u7075\u6d3b\u6027\u548c\u8d2a\u5fc3\u7b97\u6cd5\u7684\u5c40\u90e8\u5206\u6790\u80fd\u529b\uff0c\u4ece\u800c\u5b9e\u73b0\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p><strong>\u6838\u5fc3\u6982\u5ff5<\/strong><\/p>\n<ul>\n<li><strong>\u8d2a\u5fc3<\/strong>\uff1a\u5728\u6bcf\u4e2a\u6b65\u9aa4\u4e2d\u505a\u51fa\u5c40\u90e8\u6700\u4f73\u9009\u62e9\uff0c\u800c\u4e0d\u8003\u8651\u672a\u6765\u7684\u5f71\u54cd\u3002<\/li>\n<li><strong>\u9012\u5f52<\/strong>\uff1a\u4ee5\u6e10\u8fdb\u65b9\u5f0f\u5206\u89e3\u95ee\u9898\uff0c\u76f4\u5230\u627e\u5230\u57fa\u7840\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<p><strong>\u6280\u672f<\/strong><\/p>\n<ol>\n<li><strong>\u5b9a\u4e49\u57fa\u7840\u6848\u4f8b\uff1a<\/strong>\u786e\u5b9a\u95ee\u9898\u4f55\u65f6\u53ef\u4ee5\u8f7b\u677e\u89e3\u51b3\u3002<\/li>\n<li><strong>\u9012\u5f52\u8c03\u7528\uff1a<\/strong>\u5c06\u95ee\u9898\u5206\u89e3\u6210\u66f4\u5c0f\u7684\u5b50\u95ee\u9898\uff0c\u5e76\u9012\u5f52\u8c03\u7528\u7b97\u6cd5\u3002<\/li>\n<li><strong>\u5408\u5e76\u7ed3\u679c\uff1a<\/strong>\u5c06\u5b50\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u5408\u5e76\u4ee5\u83b7\u5f97\u539f\u59cb\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/li>\n<li><strong>\u8d2a\u5fc3\u9009\u62e9\uff1a<\/strong>\u5728\u6bcf\u4e2a\u9012\u5f52\u6b65\u9aa4\u4e2d\uff0c\u4ece\u53ef\u7528\u7684\u9009\u9879\u4e2d\u9009\u62e9\u5c40\u90e8\u6700\u4f73\u9009\u62e9\u3002<\/li>\n<\/ol>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u80cc\u5305\u95ee\u9898<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u80cc\u5305\u95ee\u9898\uff0c\u5176\u4e2d\u6709 n \u4ef6\u7269\u54c1\uff0c\u6bcf\u4ef6\u7269\u54c1\u6709\u91cd\u91cf\u548c\u4ef7\u503c\u3002\u6211\u4eec\u9700\u8981\u627e\u5230\u88c5\u5165\u80cc\u5305\u7684\u7269\u54c1\u7ec4\u5408\uff0c\u4f7f\u5f97\u603b\u4ef7\u503c\u6700\u5927\u5316\uff0c\u540c\u65f6\u4e0d\u8d85\u8fc7 \u80cc\u5305\u5bb9\u91cf\u3002<\/p>\n<p><strong>Java \u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>import java.util.List;\n\nclass Item {\n  int weight;\n  int value;\n\n  public Item(int weight, int value) {\n    this.weight = weight;\n    this.value = value;\n  }\n}\n\npublic class Backpack {\n\n  public static int maxValue(List&lt;Item&gt; items, int capacity) {\n    return maxValue(items, capacity, 0);\n  }\n\n  private static int maxValue(List&lt;Item&gt; items, int capacity, int index) {\n    if (index &gt;= items.size() || capacity &lt;= 0) {\n      return 0;\n    }\n\n    Item item = items.get(index);\n\n    int takeItem = 0;\n    if (item.weight &lt;= capacity) {\n      takeItem = item.value + maxValue(items, capacity - item.weight, index + 1);\n    }\n\n    int leaveItem = maxValue(items, capacity, index + 1);\n\n    return Math.max(takeItem, leaveItem);\n  }\n}<\/pre>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u9012\u5f52\u5f0f\u8d2a\u5fc3\u7b97\u6cd5\u6765\u89e3\u51b3\u80cc\u5305\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u57fa\u7840\u6848\u4f8b\uff1a\u5f53\u7269\u54c1\u5168\u90e8\u7528\u5b8c\u6216\u80cc\u5305\u5bb9\u91cf\u4e3a 0 \u65f6\uff0c\u6700\u5927\u4ef7\u503c\u4e3a 0\u3002<\/li>\n<li>\u9012\u5f52\u8c03\u7528\uff1a\u5728\u6bcf\u4e2a\u9012\u5f52\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u9012\u5f52\u5730\u8c03\u7528\u7b97\u6cd5\uff0c\u5206\u522b\u5305\u62ec\u5f53\u524d\u7269\u54c1\u548c\u4e0d\u5305\u62ec\u5f53\u524d\u7269\u54c1\u7684\u60c5\u51b5\u3002<\/li>\n<li>\u8d2a\u5fc3\u9009\u62e9\uff1a\u6211\u4eec\u9009\u62e9\u5c40\u90e8\u6700\u4f73\u9009\u62e9\uff0c\u5373\u5f53\u524d\u7269\u54c1\u91cd\u91cf\u4e0d\u8d85\u8fc7\u80cc\u5305\u5bb9\u91cf\u65f6\uff0c\u5c06\u5f53\u524d\u7269\u54c1\u7684\u4ef7\u503c\u52a0\u4e0a\u540e\u7eed\u7269\u54c1\u7684\u6700\u5927\u4ef7\u503c\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4f7f\u7528\u6b64\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u627e\u5230\u88c5\u5165\u80cc\u5305\u7684\u6700\u5927\u4ef7\u503c\u7269\u54c1\u7ec4\u5408\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u9012\u5f52\u5f0f\u8d2a\u5fc3\u7b97&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203011","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203011"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203011\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}