题解 | #输入整型数组和排序标识,对其元素进行排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
String num = in.nextLine();
String arr = in.nextLine();
final String rule = in.nextLine();
String[] arrs = arr.split(" ");
List<Integer> a = new ArrayList<>();
for (String s : arrs) {
a.add(Integer.valueOf(s));
}
//System.out.println(a);
Collections.sort(a, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if ("0".equals(rule))return o1 - o2;
return o2 - o1;
}
});
System.out.println(a.toString().replaceAll(", ", " ").replace("[",
"").replace("]", ""));
}
}
}
查看25道真题和解析