题解 | #明明的随机数#
明明的随机数
https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<Integer> list = new ArrayList<>();
Set<Integer> set = new HashSet<>();
int sum = in.nextInt();
//System.out.println("sum = " + sum);
for (int i = 0; i < sum; i++) {
int ey = in.nextInt();
set.add(ey);
}
for (Integer t : set) {
list.add(t);
}
Collections.sort(list);
for (int c = 0; c < list.size(); c++) {
System.out.println(list.get(c));
}
}
}
利用Set 的不可重复性将输入的元素收集起来,再通过遍历添加到List集合,利用集合自带的排序方法进行排序后遍历输出。


查看11道真题和解析