{"id":553,"date":"2018-06-17T14:16:24","date_gmt":"2018-06-17T06:16:24","guid":{"rendered":"http:\/\/prayerlaputa.com\/?p=553"},"modified":"2019-07-17T14:24:39","modified_gmt":"2019-07-17T06:24:39","slug":"java%e7%94%9f%e6%88%90%e6%80%bb%e5%92%8c%e5%8f%8a%e4%b8%aa%e6%95%b0%e7%a1%ae%e5%ae%9a%e7%9a%84%e4%b8%80%e7%bb%84%e9%9a%8f%e6%9c%ba%e6%95%b0%e3%80%90%e8%bd%ac%e8%bd%bd%e3%80%91","status":"publish","type":"post","link":"http:\/\/prayerlaputa.com\/?p=553","title":{"rendered":"JAVA\u751f\u6210\u603b\u548c\u53ca\u4e2a\u6570\u786e\u5b9a\u7684\u4e00\u7ec4\u968f\u673a\u6570\u3010\u8f6c\u8f7d\u3011"},"content":{"rendered":"\r\n<p>JAVA\u751f\u6210\u603b\u548c\u53ca\u4e2a\u6570\u786e\u5b9a\u7684\u4e00\u7ec4\u968f\u673a\u6570<\/p>\r\n<p>\u8f6c\u8f7d\u5728\u535a\u5ba2\u6587\u7ae0 <a href=\"https:\/\/blog.csdn.net\/i_chenjiahui\/article\/details\/54947198\">JAVA\u751f\u6210\u603b\u548c\u53ca\u4e2a\u6570\u786e\u5b9a\u7684\u4e00\u7ec4\u968f\u673a\u6570<\/a><\/p>\r\n<p><!--more--><\/p>\r\n\r\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.ArrayList;\r\nimport java.util.Arrays;\r\nimport java.util.Comparator;\r\nimport java.util.Random;\r\n\r\npublic class Test {\r\n \r\n\tpublic static void main(String[] args) {\r\n \r\n\t\tsplitRedPacket(40000, 41, 300, 1500);\r\n\t\tSystem.out.println(&quot;*****************&quot;);\r\n\t\tsplitRedPacket(20000, 30, 300, 1000);\r\n \r\n\t}\r\n \r\n\t\/**\r\n\t * \r\n\t * @param total\r\n\t *            \u603b\u91d1\u989d\r\n\t * @param splitCount\r\n\t *            \u4e2a\u6570\r\n\t * @param min\r\n\t *            \u6700\u5c0f\u91d1\u989d\r\n\t * @param max\r\n\t *            \u6700\u5927\u91d1\u989d\r\n\t *\/\r\n\tpublic static void splitRedPacket(int total, int splitCount, int min, int max) {\r\n\t\tSystem.out.println(&quot;\u603b\u91d1\u989d\uff1a\t&quot; + total);\r\n\t\tSystem.out.println(&quot;\u4e2a\u6570\uff1a\t&quot; + splitCount);\r\n\t\tSystem.out.println(&quot;\u6700\u5c0f\u91d1\u989d\uff1a\t&quot; + min);\r\n\t\tSystem.out.println(&quot;\u6700\u5927\u91d1\u989d\uff1a\t&quot; + max);\r\n \r\n\t\tArrayList&lt;Integer&gt; al = new ArrayList&lt;Integer&gt;();\r\n\t\tRandom random = new Random();\r\n \r\n\t\tif ((splitCount &amp; 1) == 1) {\/\/ \u5947\u6570\u4e2a\u7ea2\u5305\uff0c\u9700\u8981\u5355\u72ec\u5c06\u5176\u4e2d\u4e00\u4e2a\u7ea2\u5305\u5148\u751f\u6210\uff0c\u4ee5\u4fdd\u8bc1\u540e\u7eed\u7b97\u6cd5\u62c6\u5206\u4efd\u6570\u4e3a\u5076\u6570\u3002\r\n\t\t\tSystem.out.println(&quot;\u7ea2\u5305\u4e2a\u6570&quot; + splitCount + &quot;\u662f\u5947\u6570\uff0c\u5355\u72ec\u751f\u6210\u4e00\u4e2a\u7ea2\u5305&quot;);\r\n\t\t\tint num = 0;\r\n\t\t\tdo {\r\n\t\t\t\tnum = random.nextInt(max);\r\n\t\t\t\t\/\/ num = (total - num) % (splitCount \/ 2) + num; \/\/\r\n\t\t\t\t\/\/ \u5c06\u540e\u9762\u7b97\u6cd5\u62c6\u5206\u65f6\u7684\u4f59\u6570\u52a0\u5165\u5230\u8fd9\u4e2a\u968f\u673a\u503c\u4e2d\u3002\r\n\t\t\t\tSystem.out.println(&quot;\u5355\u4e2a\u7684\u968f\u673a\u7ea2\u5305\u4e3a\uff1a&quot; + num);\r\n\t\t\t} while (num &gt;= max || num &lt;= min);\r\n \r\n\t\t\ttotal = 40000 - num;\r\n\t\t\tal.add(num);\r\n\t\t}\r\n\t\tint couples = splitCount &gt;&gt; 1;\r\n\t\tint perCoupleSum = total \/ couples;\r\n \r\n\t\tif ((splitCount &amp; 1) == 1) {\r\n\t\t\tSystem.out.println(&quot;\u5904\u7406\u540e\u5269\u4f59\u7684\u91d1\u989d\u4e3a\uff1a&quot; + total);\r\n\t\t}\r\n\t\tSystem.out.println(&quot;\u5c06&quot; + total + &quot;\u5143\u62c6\u5206\u4e3a&quot; + couples + &quot;\u5bf9\u91d1\u989d\uff0c\u6bcf\u5bf9\u603b\u989d\uff1a&quot; + perCoupleSum);\r\n \r\n\t\tfor (int i = 0; i &lt; couples; i++) {\r\n\t\t\tBoolean finish = true;\r\n\t\t\tint num1 = 0;\r\n\t\t\tint num2 = 0;\r\n\t\t\tdo {\r\n\t\t\t\tnum1 = random.nextInt(max);\r\n\t\t\t\tnum2 = perCoupleSum - num1;\r\n\t\t\t\tif (!al.contains(num1) &amp;&amp; !al.contains(num2)) {\r\n\t\t\t\t\tif (i == 0) {\r\n\t\t\t\t\t\tnum1 = (total - couples * perCoupleSum) + num1;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t} while (num1 &lt; min || num1 &gt; max || num2 &lt; min || num2 &gt; max);\r\n\t\t\tal.add(num1);\r\n\t\t\tal.add(num2);\r\n\t\t}\r\n \r\n\t\tint check_num = 0;\r\n\t\tInteger.compare(1, 2);\r\n\t\tal.sort(new Comparator&lt;Integer&gt;() {\r\n\t\t\t@Override\r\n\t\t\tpublic int compare(Integer o1, Integer o2) {\r\n\t\t\t\treturn Integer.compare(o1, o2);\r\n\t\t\t}\r\n\t\t});\r\n \r\n\t\tSystem.out.println(Arrays.toString(al.toArray()));\r\n \r\n\t\tfor (int x : al) {\r\n\t\t\tcheck_num = check_num + x;\r\n\t\t}\r\n\t\tSystem.out.println(&quot;\u9a8c\u8bc1\u603b\u548c\uff1a&quot; + check_num);\r\n\t}\r\n}\r\n<\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>JAVA\u751f\u6210\u603b\u548c\u53ca\u4e2a\u6570\u786e\u5b9a\u7684\u4e00\u7ec4\u968f\u673a\u6570 \u8f6c\u8f7d\u5728\u535a\u5ba2\u6587\u7ae0 JAVA\u751f\u6210\u603b\u548c\u53ca\u4e2a\u6570\u786e\u5b9a\u7684\u4e00\u7ec4\u968f\u673a\u6570<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[6,14],"tags":[33],"_links":{"self":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/553"}],"collection":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=553"}],"version-history":[{"count":5,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":559,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions\/559"}],"wp:attachment":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}