题解 | #对整形数组进行升序或者降序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
// 本题为考试多行输入输出规范示例,无需提交,不计分。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Scanner sc = new Scanner(System.in);
// 元素个数
String num = br.readLine();
// 数组元素
String line = br.readLine();
// 0或1(0升1降)
int ascAndDesc = Integer.parseInt(br.readLine());
String[] split = line.split(" ");
int n = Integer.parseInt(num);
int[] numArr = new int[n];
for (int i = 0; i < n; i++) {
numArr[i] = Integer.parseInt(split[i]);
}
Arrays.sort(numArr);
if (ascAndDesc == 0) {
for (int i : numArr) {
System.out.print(i + " ");
}
}
if (ascAndDesc == 1) {
for (int i = numArr.length - 1; i >= 0; i--) {
System.out.print(numArr[i] + " ");
}
}
}
}
#刷题#
