题解 | #输入整型数组和排序标识,对其元素按照升序或降序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
import java.util.*; import java.util.stream.Collectors; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { sort(); } /** * HJ101 输入整型数组和排序标识,对其元素按照升序或降序进行排序 */ private static void sort() { Scanner in = new Scanner(System.in); int num = in.nextInt(); List<Integer> lst = new ArrayList<>(); while (num > 0) { lst.add(in.nextInt()); num--; } int sort = in.nextInt(); lst.sort(Comparator.naturalOrder()); if (sort > 0) { lst.sort(Collections.reverseOrder()); } System.out.println(lst.stream().map(String::valueOf).collect(Collectors.joining(" "))); } }