题解 | #明明的随机数#

明明的随机数

https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            test6(in);
        }
    }

    public static void test1(Scanner in) {
        int n = in.nextInt();
        Set<Integer> set = new TreeSet<>();
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            set.add(a);
        }
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    public static void test2(Scanner in) {
        int n = in.nextInt();
        int[] temp = new int[500];
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            temp[a - 1] = 1;
        }
        for (int i = 0; i < 500; i++) {
            if (temp[i] == 1) {
                System.out.println(i + 1);
            }
        }
    }

    public static void test3(Scanner in) {
        int n = in.nextInt();
        int[] temp = new int[n];
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            temp[i] = a;
        }
        Arrays.sort(temp);
        System.out.println(temp[0]);
        for (int i = 1; i < temp.length; i++) {
            if (temp[i-1] != temp[i]) {
                System.out.println(temp[i]);
            }
        }
    }

    public static void test4(Scanner in) {
        int n = in.nextInt();
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            set.add(a);
        }
        Object[] temp = set.toArray();
        Arrays.sort(temp);
        for (int i = 0; i < temp.length; i++) {
            System.out.println(temp[i]);
        }
    }

    public static void test5(Scanner in) {
        int n = in.nextInt();
        int[] temp = new int[n];
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            temp[i] = a;
        }
        for (int i = 0;i<n;i++) {
            for (int j = i+1;j<n;j++){
                if(temp[i]>temp[j]){
                    int t = temp[i];
                    temp[i] = temp[j];
                    temp[j] = t;
                } else if (temp[i] == temp[j]) {
                    temp[j] = 0;
                }
            }
        }
        
        for(int tem : temp) {
            if (tem != 0) {
                System.out.println(tem);
            }
        }
    }

    public static void test6(Scanner in) {
        int n = in.nextInt();
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            set.add(a);
        }
        List<Integer> list = new ArrayList<Integer>(set);
        // list.sort(Comparator.naturalOrder());
        list.sort((o1,o2) -> o1 > o2 ? 1 : -1);
        list.forEach(System.out::println);
        
    }

    public static void test7(Scanner in) {

    }
}

全部评论

相关推荐

LZHR:老哥你从投递简历测评完到一面中间隔了多久呀,我这边已经过了五天了仍显示简历筛选中是不是就是挂了
腾讯求职进展汇总
点赞 评论 收藏
分享
03-01 19:30
已编辑
南京大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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