题解 | #输入整型数组和排序标识,升序或降序排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int number = in.nextInt(); int[] ins = new int[number]; for(int i=0;i<number;i++){ ins[i] = in.nextInt(); } int sort = in.nextInt(); Arrays.sort(ins); if(sort==0){ // asc for(int i=0;i<ins.length;i++){ System.out.print(ins[i]+" "); } } else { // desc for(int i=ins.length-1;i>=0;i--){ System.out.print(ins[i]+" "); } } } }
知识点:
排序 Arrays.sort(ins);