{"id":1252,"date":"2022-06-03T08:55:07","date_gmt":"2022-06-03T00:55:07","guid":{"rendered":"https:\/\/sanlangcode.com\/?p=1252"},"modified":"2022-06-03T08:55:07","modified_gmt":"2022-06-03T00:55:07","slug":"hj101-%e8%be%93%e5%85%a5%e6%95%b4%e5%9e%8b%e6%95%b0%e7%bb%84%e5%92%8c%e6%8e%92%e5%ba%8f%e6%a0%87%e8%af%86%ef%bc%8c%e5%af%b9%e5%85%b6%e5%85%83%e7%b4%a0%e6%8c%89%e7%85%a7%e5%8d%87%e5%ba%8f%e6%88%96","status":"publish","type":"post","link":"https:\/\/sanlangcode.com\/index.php\/2022\/06\/03\/hj101-%e8%be%93%e5%85%a5%e6%95%b4%e5%9e%8b%e6%95%b0%e7%bb%84%e5%92%8c%e6%8e%92%e5%ba%8f%e6%a0%87%e8%af%86%ef%bc%8c%e5%af%b9%e5%85%b6%e5%85%83%e7%b4%a0%e6%8c%89%e7%85%a7%e5%8d%87%e5%ba%8f%e6%88%96\/","title":{"rendered":"HJ101 \u8f93\u5165\u6574\u578b\u6570\u6807\u8bc6\u6392\u5e8fComparator"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u63cf\u8ff0<\/h2>\n\n\n\n<p>\u8f93\u5165\u6574\u578b\u6570\u7ec4\u548c\u6392\u5e8f\u6807\u8bc6\uff0c\u5bf9\u5176\u5143\u7d20\u6309\u7167\u5347\u5e8f\u6216\u964d\u5e8f\u8fdb\u884c\u6392\u5e8f<br>\u6570\u636e\u8303\u56f4\uff1a&nbsp;1\u2264<em>n<\/em>\u22641000&nbsp;&nbsp;\uff0c\u5143\u7d20\u5927\u5c0f\u6ee1\u8db3&nbsp;0\u2264<em>val<\/em>\u2264100000&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u5165\u63cf\u8ff0\uff1a<\/h3>\n\n\n\n<p>\u7b2c\u4e00\u884c\u8f93\u5165\u6570\u7ec4\u5143\u7d20\u4e2a\u6570<br>\u7b2c\u4e8c\u884c\u8f93\u5165\u5f85\u6392\u5e8f\u7684\u6570\u7ec4\uff0c\u6bcf\u4e2a\u6570\u7528\u7a7a\u683c\u9694\u5f00<br>\u7b2c\u4e09\u884c\u8f93\u5165\u4e00\u4e2a\u6574\u65700\u62161\u30020\u4ee3\u8868\u5347\u5e8f\u6392\u5e8f\uff0c1\u4ee3\u8868\u964d\u5e8f\u6392\u5e8f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u8f93\u51fa\u63cf\u8ff0\uff1a<\/h3>\n\n\n\n<p>\u8f93\u51fa\u6392\u597d\u5e8f\u7684\u6570\u5b57<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u793a\u4f8b1<\/h2>\n\n\n\n<p>\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">8\n1 2 4 9 3 55 64 25\n0<\/pre>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1 2 3 4 9 25 55 64<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\npublic class Main{\n\tpublic static void main(String&#91;] args){\n\t\tScanner sc = new Scanner(System.in);\n\t\twhile(sc.hasNext()){\n\t\t\tint n = sc.nextInt();\/\/\u63a5\u6536\u6570\u7ec4\u957f\u5ea6\n\t\t\tint&#91;] arr = new int&#91;n];\/\/\u521b\u5efa\u6570\u7ec4\n\n\t\t\tfor (int i = 0; i &lt; n; i++) {\/\/\u6570\u7ec4\u586b\u5165\n\t\t\t\tarr&#91;i] = sc.nextInt();\n\t\t\t}\n\t\t\t\n\t\t\tint flag = sc.nextInt();\/\/\u63a5\u6536\u6392\u5e8f\u6807\u8bc6\n\t\t\tArrays.sort(arr);\/\/\u6570\u7ec4\u6392\u5e8f\n\n\t\t\tif (flag == 0) {\/\/\u6b63\u5e8f\u8f93\u51fa\n\t\t\t\tfor(int i =0; i &lt; arr.length; i++){\n\t\t\t\t\tSystem.out.print(arr&#91;i] + \" \");\n\t\t\t\t} \n\t\t\t}\n\t\t\telse {\/\/\u9006\u5e8f\u8f93\u51fa\n\t\t\t\t\tfor(int i = arr.length - 1; i &gt;= 0; i--){\n\t\t\t\t\t\tSystem.out.print(arr&#91;i] + \" \");\n\t\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Scanner\u6709\u591a\u6b21\u8f93\u5165\uff0c\u4e14\u5b58\u5728\u8f93\u5165\u6570\u5b57\u7ec4\u6210\u6570\u7ec4\u7684\u60c5\u51b5 \u5efa\u8bae\u4f7f\u7528while(sc.hasNext()) \u5224\u65ad\u662f\u5426\u5b58\u5728\u4e0b\u6b21\u8f93\u5165\uff0c\u5e76\u6309\u4e1a\u52a1\u5f55\u5165\u6570\u5b57\u7ec4\u6210\u6570\u7ec4<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Arrays.sort\u65b9\u6cd5\u53ef\u4ee5\u5bf9Integer\u6570\u7ec4 \u6309Comparator\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\u6267\u884c\u6392\u5e8f\uff0c\u6ce8\u610f\u53c2\u6570o1,o2, \u5de6-\u53f3\uff08o1-o2\uff09\u662f\u5347\u5e8f\u6392\u5e8f; \u53f3-\u5de6\uff08o2-o1\uff09\u662f\u964d\u5e8f\u6392\u5e8f;<\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\npublic class Main{\n    public static void main(String&#91;] args){\n        Scanner sc = new Scanner(System.in);\n   \n        \/\/\u6709\u591a\u6b21\u8f93\u5165\uff0c\u4e14\u5b58\u5728\u8f93\u5165\u6570\u5b57\u7ec4\u6210\u6570\u7ec4\u7684\u60c5\u51b5\n        while(sc.hasNext()){\n            \/\/\u7b2c\u4e00\u884c\u8f93\u5165\u6570\u7ec4\u5143\u7d20\u4e2a\u6570\n             int arrCount = sc.nextInt();\n            \/\/\u7b2c\u4e8c\u884c\u8f93\u5165\u5f85\u6392\u5e8f\u7684\u6570\u7ec4\uff0c\u6bcf\u4e2a\u6570\u7528\u7a7a\u683c\u9694\u5f00\n            Integer&#91;] arr = new Integer&#91;arrCount];\n            \/\/\u5f55\u5165 arrCount \u4e2a \u6570\u5b57\uff0c\u7ec4\u6210\u6570\u7ec4\n            for(int i=0;i&lt;arrCount;i++){\n                arr&#91;i] = sc.nextInt();\n            }\n            \/\/\u7b2c\u4e09\u884c\u8f93\u5165\u4e00\u4e2a\u6574\u65700\u62161\u30020\u4ee3\u8868\u5347\u5e8f\u6392\u5e8f\uff0c1\u4ee3\u8868\u964d\u5e8f\u6392\u5e8f\n            int flagSort = sc.nextInt();\n            \n            if(flagSort==0){\n                Arrays.sort(arr,new Comparator&lt;Integer&gt;(){\n                    public int compare(Integer o1 ,Integer o2){\n                        return o1-o2;\n                    }\n                });\n            }\n            if(flagSort==1){\n                Arrays.sort(arr,new Comparator&lt;Integer&gt;(){\n                    public int compare(Integer o1 ,Integer o2){\n                        return o2-o1;\n                    }\n                });\n            }\n      \n            for(Integer m : arr){\n                System.out.print(m + \" \");\n            }\n            break;\n        }\n        System.out.println();\n  \n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u63cf\u8ff0 \u8f93\u5165\u6574\u578b\u6570\u7ec4\u548c\u6392\u5e8f\u6807\u8bc6\uff0c\u5bf9&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1253,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,4],"tags":[],"class_list":["post-1252","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-4"],"_links":{"self":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/1252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=1252"}],"version-history":[{"count":0,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/1252\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media\/1253"}],"wp:attachment":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=1252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=1252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=1252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}