Passion and Patience

Work Hard, Play Hard

导航

HJ3 明明的随机数

利用容器的自排序API方法Collections.sort()

注意list也有list.contains()方法

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);
        // 注意 hasNext 和 hasNextLine 的区别
        int N = Integer.parseInt(in.nextLine());
        // int []nums = new int[N];
        List<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i<N;i++){
            int num = Integer.parseInt(in.nextLine()) ;
            if(!list.contains(num)){
                list.add(num);
            }
        }
        Collections.sort(list);
        list.forEach(item->System.out.println(item));
    }
}

待用快速排序法解决:不断建立基准,将小于基准的元素挪到左边,大于基准的元素挪到右边

posted on 2024-04-05 23:27  安静的聆  阅读(23)  评论(0)    收藏  举报