题解 | #明明的随机数#
明明的随机数
https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); List<Integer> list = new ArrayList<>(); int l = in.nextInt(); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int a = in.nextInt(); if(!list.contains(a)){ list.add(a); } } list.sort(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { //o1 代表后面一个数 o2代表前面一个数 if (o1 < o2)return -1; //-1表示交换两个数的位置 所以这里实现的是降序排列 else return 1; } }); for(Integer a : list){ System.out.println(a); } } }