{"id":1516,"date":"2020-02-22T18:20:48","date_gmt":"2020-02-22T10:20:48","guid":{"rendered":"http:\/\/xiunian.wang\/?p=1516"},"modified":"2021-09-23T00:40:01","modified_gmt":"2021-09-22T16:40:01","slug":"%e5%9b%9b%e4%b8%aa%e4%b8%8d%e5%90%8c%e9%87%8d%e9%87%8f%e7%a0%9d%e7%a0%81%e5%8f%af%e4%bb%a5%e7%a7%b0%e5%87%ba%e5%a4%9a%e7%a7%8d%e9%87%8d%e9%87%8f","status":"publish","type":"post","link":"https:\/\/blog.xiunian.wang\/?p=1516","title":{"rendered":"\u56db\u4e2a\u4e0d\u540c\u91cd\u91cf\u781d\u7801\u53ef\u4ee5\u79f0\u51fa\u591a\u79cd\u91cd\u91cf"},"content":{"rendered":"\n<p>\u6700\u8fd1\u75ab\u60c5\u5728\u5bb6\u6ca1\u6cd5\u51fa\u95e8\uff0c\u56e0\u6b64\u5c1d\u8bd5\u53bb\u627e\u627e\u5de5\u4f5c\uff0c\u6295\u9012\u4e86\u4e00\u4e9b\u5927\u5382\u7684\u804c\u4f4d\uff0c\u4e5f\u6709\u4e00\u4e9b\u597d\u5fc3\u730e\u5934\u5e2e\u6211\u63a8\u8350\u3002\u4f46\u662f\u5b9e\u5728\u662f\u4e0d\u597d\u610f\u601d\uff0c\u4e00\u9762\u90fd\u8fc7\u4e0d\u4e86\u3002 \u4e0d\u8fc7\u6211\u4f3c\u4e4e\u4e5f\u6ca1\u6709\u592a\u5927\u671f\u5f85\u4e86\u3002\u6bd5\u7adf\u8fd9\u73a9\u610f\u5bf9\u4e8e\u6211\u6765\u8bf4\uff0c\u8fd8\u662f\u770b\u8fd0\u6c14\u5566\uff0c\u53ea\u662f\u7eaf\u5f53\u627e\u4e2a\u4eba\u8bf4\u8bf4\u8bdd\uff0c\u987a\u4fbf\u8fd8\u80fd\u804a\u804a\u770b\u81ea\u5df1\u5982\u679c\u627e\u5de5\u4f5c\u8fd8\u6709\u54ea\u4e9b\u4e0d\u8db3\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\u817e\u8baf\u97f3\u4e50\u524d\u5929\u6253\u7535\u8bdd\u9762\u8bd5\uff0c\u6700\u540e\u95ee\u4e86\u6211\u4e00\u4e2a\u7b97\u6cd5\u95ee\u9898\uff0c\u56db\u4e2a\u781d\u7801\u53ef\u4ee5\u79f0\u51fa\u591a\u5c11\u79cd\u91cd\u91cf\u3002<\/p>\n\n\n\n<p>\u6211\u5f53\u65f6\u542c\u5230\u781d\u7801\u8fd9\u4e24\u4e2a\u5b57\u7adf\u7136\u6ca1\u6709\u53cd\u5e94\u8fc7\u6765\uff0c\u8fd8\u95ee\u4e86\u4e0b\u781d\u7801\u662f\u4ec0\u4e48\uff0c\u5929\u5e73\u4e0a\u90a3\u4e2a\u5417\uff1f\u611f\u89c9\u51e0\u767e\u5e74\u6ca1\u6709\u542c\u5230\u8fc7\u8fd9\u4e2a\u8bcd\u4e86\uff0c\u6709\u70b9\u61f5\u903c\u3002\u9762\u8bd5\u7684\u8ba9\u6211\u8bf4\u601d\u8def\uff0c\u6211\u60f3\u8fd9\u4e0d\u5c31\u662f\u4e00\u4e2a\u6570\u5b66\u9898\u5417\uff1f\u6392\u5217\u7ec4\u5408\u60f3\u4e86\u4e00\u4e0b\uff0c\u4f46\u8f6c\u5ff5\u4e00\u60f3 \u4ed6\u6ca1\u544a\u8bc9\u6211\u91cd\u91cf\uff0c\u6211\u600e\u4e48\u77e5\u9053\u53ef\u4ee5\u79f0\u51fa\u591a\u5c11\u79cd\u91cd\u91cf\u5462\uff1f \u6bd4\u5982 2,4,6,8 \u53ef\u4ee5\u75282\u548c4 \u79f0\u51fa6\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e00\u4e2a6 \u79f0\u51fa6 \u5440\u3002\u90a3\u8fd9\u7b97\u4e00\u79cd\u5417\uff1f\u597d\u50cf\u8fd8\u4e0d\u5bf9\uff0c\u6211\u8fd8\u53ef\u4ee5\u7528 \u4e00\u4e2a2 \u79f0\u51fa\u4e00\u4e2a\u4e8c\uff0c\u7528\u4e00\u4e2a4\uff0c\u548c6 \uff0c\u628a\u7269\u54c1\u653e\u57284\u7684\u90a3\u4e00\u8fb9\u79f0\u51fa\u4e00\u4e2a\u4e8c\u7684\u91cd\u91cf\u6765\u5440\u3002\u6211\u9760\uff0c\u8fd9\u4e8b\u60c5\u592a\u590d\u6742\u4e86\u3002\u7136\u540e\u5c31\u4e71\u4e86\u3002\u6700\u540e\u6302\u65ad\u7535\u8bdd\uff0c\u9762\u8bd5\u7ed3\u675f\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p> \u540e\u6765\u6211\u60f3\u4e86\u60f3C44+C43+C42+C41 =15 \u5440\uff0c\u8fd9\u8fd8\u8981\u4ec0\u4e48\u601d\u8def\u5417\uff1f \u60f3\u8d77\u4ed6\u8bf4\u7684\u662f\u7b97\u6cd5\uff1f\u8fd9tm\u662f\u8ba9\u6211\u8bf4\u7b97\u6cd5\u5427\u3002\u8fd9\u4e0d\u5c31\u662f\u627e\u51fa\u4e00\u4e2a\u6570\u7ec4\u4e2d\u6709\u591a\u5c11\u4e2a\u5b50\u6570\u7ec4\u5417\u3002\u81f3\u4e8e\u653e\u5230\u4e24\u8fb9\u7684\u505a\u6cd5\u5e94\u8be5\u662f\u4e0d\u9700\u8981\u8003\u8651\u5427\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u95ee\u9898\u7b80\u5316\u4e3a\u6c42\u7684\u4e00\u4e2a\u6570\u7ec4\u4e2d\u5b50\u6570\u7ec4\u7684\u7ec4\u5408\u65b9\u5f0f\u6709\u591a\u5c11\u3002<\/p>\n\n\n\n<p>\u6211\u665a\u4e0a\u82b1\u4e86\u4e00\u4e9b\u65f6\u95f4\u5199\u4e86\u4e00\u4e0b\u3002\u91c7\u7528\u7684\u662f\u52a8\u6001\u89c4\u5212\u7684\u65b9\u5f0f\u3002 <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.*;\n\n\/\/\u7ed9\u5b9a\u82e5\u5e72\u4e2a\u781d\u7801\uff0c\u6c42\u8d77\u80fd\u591f\u79f0\u51fa\u591a\u5c11\u79cd\u91cd\u91cf\npublic class WeightSolution {\n\n    private List&lt;int[]> getSubArray(int[] weights) {\n        List&lt;int[]> result = new LinkedList&lt;>();\n        for (int i = 1; i &lt;= weights.length; i++) {\n            result.addAll(getSubBySize(i, weights));\n        }\n        return result;\n    }\n\n    private List&lt;int[]> getSubBySize(int size, int[] weights) {\n        List&lt;int[]> result = new LinkedList&lt;>();\n        List&lt;int[]> found = find(size, weights, 0, weights.length);\n        for (int[] ints : found) {\n            int[] values = new int[ints.length];\n            for (int i = 0; i &lt; ints.length; i++) {\n                values[i] = weights[ints[i]];\n            }\n            result.add(values);\n        }\n        return result;\n    }\n\n\n    \/\/\u8fd4\u56de\u6570\u7ec4\u7684\u4e0b\u6807\u7684\u6570\u7ec4\n    private List&lt;int[]> find(int size, int[] weights, int start, int end) {\n        List&lt;int[]> result = new LinkedList&lt;>();\n        if (size == 1) {\n            for (int i = start; i &lt; end; i++) {\n                result.add(new int[]{i});\n            }\n\n            return result;\n        }\n\n        List&lt;int[]> cached = new LinkedList&lt;>();\n        List&lt;int[]> rs = find(size - 1, weights, 0, weights.length);\n        for (int i = 0; i &lt; weights.length; i++) {\n            int first = i;\n            for (int[] r : rs) {\n                if (notIn(first, r)) {\n                    int[] newS = new int[r.length + 1];\n                    newS[0] = first;\n                    System.arraycopy(r, 0, newS, 1, r.length);\n                    if (!isExist(newS, cached)) {\n                        result.add(newS);\n                        cached.add(newS);\n                    } else {\n\/\/                        System.out.println(\"\u5df2\u5b58\u5728:\" + changeToStr(newS));\n                    }\n                }\n                \/\/\u53bb\u91cd\n            }\n        }\n        return result;\n    }\n\n    \/\/\u662f\u5426\u5b58\u5728\u7f13\u5b58\u4e2d\n    private boolean isExist(int[] r, List&lt;int[]> ls) {\n\n        for (int[] s : ls) {\n            if (s.length != r.length) {\n                continue;\n            }\n            int[][] matrix = new int[s.length][s.length];\n            int[] a = r;\n            int[] b = s;\n            for (int i = 0; i &lt; matrix.length; i++) {\n                for (int j = 0; j &lt; matrix[i].length; j++) {\n                    matrix[i][j] = (a[i] == b[j] ? 1 : 0);\n                }\n            }\n            boolean isEqual = true;\n            for (int i = 0; i &lt; matrix.length; i++) {\n                int countH = 0;\n                for (int j = 0; j &lt; matrix[i].length; j++) {\n                    if (matrix[i][j] == 1) {\n                        countH++;\n                        int countV = 0;\n                        for (int k = i; k &lt; matrix.length; k++) {\n                            if (matrix[k][j] == 1) {\n                                countV++;\n                            }\n                        }\n                        if (countV != 1) {\n                            isEqual = false;\n                            break;\n                        }\n                    }\n                }\n                if (countH != 1) {\n                    isEqual = false;\n                    break;\n                }\n            }\n            if (isEqual) {\n                return true;\n            }\n        }\n        return false;\n\n    }\n\n    private boolean notIn(int a, int[] ls) {\n        boolean in = true;\n        for (int l : ls) {\n            if (a == l) {\n                return !in;\n            }\n        }\n        return in;\n    }\n\n    public static void main(String[] args) {\n        int[] rs = new int[]{1, 2, 5, 10};\n\/\/        print(new WeightSolution().getSubBySize(2, rs));\n        print(new WeightSolution().getSubArray(rs));\n    }\n\n    public static void print(List&lt;int[]> list) {\n        for (int[] ints : list) {\n            System.out.print(Arrays.toString(ints));\n            int sum = 0;\n            for (int anInt : ints) {\n                sum += anInt;\n            }\n            System.out.print(\"->\" + sum + \";\");\n        }\n        System.out.println(\"WeightSolution.print\");\n        System.out.println(\"\u79f0\u91cd\u65b9\u6cd5\u6709 [\" + list.size() + \"]\");\n    }\n\n}<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"518\" height=\"534\" src=\"\/wp-content\/uploads\/2020\/02\/WX20200222-180807@2x.png\" alt=\"\" class=\"wp-image-1519\"\/><\/figure>\n\n\n\n<p>\u6838\u5fc3\u601d\u60f3\u662f\uff0c\u7528\u6570\u7ec4\u89d2\u6807\u5f62\u6210\u7684\u6570\u7ec4\u4ee3\u8868dp\u7684\u89e3\uff0c\u5982\u679c\u6709N\u4e2a\u5143\u7d20\u7684\u6570\u7ec4S\uff0c<\/p>\n\n\n\n<p>\u4ee51\u4e3a\u957f\u5ea6\u5b50\u5143\u7d20\u89e3\u4e3a\uff1a<\/p>\n\n\n\n<p>dp(1)={[0],[1],&#8230;[n-1]}<\/p>\n\n\n\n<p>\u4ee52 \u4e3a\u957f\u5ea6\u7684\u89e3\u4e3a\uff1a<\/p>\n\n\n\n<p>Si*dp(1)  where Si not in dp(1), <\/p>\n\n\n\n<p>\u4ee5\u6b64\u7c7b\u63a8\uff0c<\/p>\n\n\n\n<p> dp(n)=dp(n-1)*Si where Si not in dp(n-1) , i=0&#8230;n<\/p>\n\n\n\n<p>\u8fd9\u6837\u5c31\u6784\u6210\u4e86\u4e00\u4e2a\u72b6\u6001\u8f6c\u79fb\u65b9\u7a0b\u3002 <\/p>\n\n\n\n<p>\u6211\u7684\u7a0b\u5e8f\u662f\u81ea\u9876\u5411\u4e0b\u6c42\u7684\u89e3\uff0c\u5f53\u7136\u8fd8\u6709\u5f88\u591a\u4f18\u5316\u7a7a\u95f4\u3002\u6ca1\u6709\u505a\u7f13\u5b58\uff0c\u5f53\u6570\u7ec4\u957f\u5ea6\u589e\u52a0\uff0c\u8ba1\u7b97\u91cf\u4e5f\u5448\u73b0\u6307\u6570\u589e\u52a0\u3002 <\/p>\n\n\n\n<p>\u5982\u679c\u9700\u8981\u8003\u8651\u653e\u7f6e\u5929\u5e73\u4e24\u8fb9\uff0c\u5219\u9700\u8981\u5728\u4ece\u5f97\u5230\u7684\u89e3\u96c6\u5408\u4e2d\u62bd\u51fa\u4efb\u610f\u4e24\u7ec4\u6ca1\u6709\u516c\u5171\u5143\u7d20\u7684\u96c6\u5408\uff0c\u518d\u8ba1\u7b97\u5404\u81ea\u603b\u548c\u7684\u5dee\u503c\uff0c\u5373\u53ef\u5f97\u5230\u66f4\u52a0\u591a\u7684\u79f0\u91cd\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u75ab\u60c5\u5728\u5bb6\u6ca1\u6cd5\u51fa\u95e8\uff0c\u56e0\u6b64\u5c1d\u8bd5\u53bb\u627e\u627e\u5de5\u4f5c\uff0c\u6295\u9012\u4e86\u4e00\u4e9b\u5927\u5382\u7684\u804c\u4f4d\uff0c\u4e5f\u6709\u4e00\u4e9b\u597d\u5fc3\u730e\u5934\u5e2e\u6211\u63a8\u8350\u3002\u4f46\u662f\u5b9e\u5728\u662f\u4e0d\u597d\u610f\u601d\uff0c\u4e00\u9762\u90fd\u8fc7\u4e0d\u4e86\u3002 \u4e0d\u8fc7\u6211\u4f3c\u4e4e\u4e5f\u6ca1\u6709\u592a\u5927\u671f\u5f85\u4e86\u3002\u6bd5\u7adf\u8fd9\u73a9\u610f\u5bf9\u4e8e\u6211\u6765\u8bf4\uff0c\u8fd8\u662f\u770b\u8fd0\u6c14\u5566\uff0c\u53ea\u662f\u7eaf\u5f53\u627e\u4e2a\u4eba\u8bf4\u8bf4\u8bdd\uff0c\u987a\u4fbf\u8fd8\u80fd\u804a\u804a\u770b\u81ea\u5df1\u5982\u679c\u627e\u5de5\u4f5c\u8fd8\u6709\u54ea\u4e9b\u4e0d\u8db3\u3002 \u5176\u4e2d\u817e\u8baf\u97f3\u4e50\u524d\u5929\u6253\u7535\u8bdd\u9762\u8bd5\uff0c\u6700\u540e\u95ee\u4e86\u6211\u4e00\u4e2a\u7b97\u6cd5\u95ee\u9898\uff0c\u56db\u4e2a\u781d\u7801\u53ef\u4ee5\u79f0\u51fa\u591a\u5c11\u79cd\u91cd\u91cf\u3002 \u6211\u5f53\u65f6\u542c\u5230\u781d\u7801\u8fd9\u4e24\u4e2a\u5b57\u7adf\u7136\u6ca1\u6709\u53cd\u5e94\u8fc7\u6765\uff0c\u8fd8\u95ee\u4e86\u4e0b\u781d\u7801\u662f\u4ec0\u4e48\uff0c\u5929\u5e73\u4e0a\u90a3\u4e2a\u5417\uff1f\u611f\u89c9\u51e0\u767e\u5e74\u6ca1\u6709\u542c\u5230\u8fc7\u8fd9\u4e2a\u8bcd\u4e86\uff0c\u6709\u70b9\u61f5\u903c\u3002\u9762\u8bd5\u7684\u8ba9\u6211\u8bf4\u601d\u8def\uff0c\u6211\u60f3\u8fd9\u4e0d\u5c31\u662f\u4e00\u4e2a\u6570\u5b66\u9898\u5417\uff1f\u6392\u5217\u7ec4\u5408\u60f3\u4e86\u4e00\u4e0b\uff0c\u4f46\u8f6c\u5ff5\u4e00\u60f3 \u4ed6\u6ca1\u544a\u8bc9\u6211\u91cd\u91cf\uff0c\u6211\u600e\u4e48\u77e5\u9053\u53ef\u4ee5\u79f0\u51fa\u591a\u5c11\u79cd\u91cd\u91cf\u5462\uff1f \u6bd4\u5982 2,4,6,8 \u53ef\u4ee5\u75282\u548c4 \u79f0\u51fa6\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e00\u4e2a6 \u79f0\u51fa6 \u5440\u3002\u90a3\u8fd9\u7b97\u4e00\u79cd\u5417\uff1f\u597d\u50cf\u8fd8\u4e0d\u5bf9\uff0c\u6211\u8fd8\u53ef\u4ee5\u7528 \u4e00\u4e2a2 \u79f0\u51fa\u4e00\u4e2a\u4e8c\uff0c\u7528\u4e00\u4e2a4\uff0c\u548c6 \uff0c\u628a\u7269\u54c1\u653e\u57284\u7684\u90a3\u4e00\u8fb9\u79f0\u51fa\u4e00\u4e2a\u4e8c\u7684\u91cd\u91cf\u6765\u5440\u3002\u6211\u9760\uff0c\u8fd9\u4e8b\u60c5\u592a\u590d\u6742\u4e86\u3002\u7136\u540e\u5c31\u4e71\u4e86\u3002\u6700\u540e\u6302\u65ad\u7535\u8bdd\uff0c\u9762\u8bd5\u7ed3\u675f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,9],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1516"}],"collection":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1516"}],"version-history":[{"count":6,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1516\/revisions"}],"predecessor-version":[{"id":1666,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=\/wp\/v2\/posts\/1516\/revisions\/1666"}],"wp:attachment":[{"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.xiunian.wang\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}