题解 | #输入整型数组和排序标识#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); List<Integer> arr = new ArrayList<>(); while (n-- > 0) { arr.add(in.nextInt()); } arr.stream().sorted(in.nextInt() == 1 ? Comparator.reverseOrder() : Comparator.naturalOrder()).forEach(x -> System.out.print(x + " ")); } }
#华为笔试#