题解 | #明明的随机数#

明明的随机数

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集合,利用集合自带的排序方法进行排序后遍历输出。

全部评论

相关推荐

09-29 00:03
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务