首页 > 试题广场 >

明明的随机数

[编程题]明明的随机数
  • 热度指数:1655188 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于明明生成的 n1500 之间的随机整数,你需要帮助他完成以下任务:
\hspace{23pt}\bullet\,删去重复的数字,即相同的数字只保留一个,把其余相同的数去掉;
\hspace{23pt}\bullet\,然后再把这些数从小到大排序,按照排好的顺序输出。
\hspace{15pt}你只需要输出最终的排序结果。

输入描述:
\hspace{15pt}第一行输入一个整数 n\ (1 \leqq n \leqq 1000),代表明明生成的数字个数。
\hspace{15pt}此后 n 行,第 i 行输入一个整数 a_i\ (1 \leqq a_i \leqq 500),代表明明生成的随机整数。


输出描述:
\hspace{15pt}输出若干行,每行输出一个整数,代表输入数据排序后的结果。第一行输出最小的数字。
示例1

输入

3
2
2
1

输出

1
2
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        // Random r = new Random();
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
//        System.out.println(i);
        List<Integer> list = new ArrayList<>();
//        int [] j = new int[i];
        for (int k = 0; k < i; k++) {
//            j[k] = r.nextInt(500);
            list.add(sc.nextInt());
        }
        Collections.sort(list);
        Set<Integer> set = new TreeSet<>(list);
        for(int k : set)
            System.out.println(k);
    }
}
这个重点是考察对复杂数据类型的操作

发表于 2025-06-10 10:25:57 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        TreeSet<Integer> numbers = new TreeSet<>();
       
        for (int i = 0; i < n; i++) {
            numbers.add(scanner.nextInt());
        }

        for (int num : numbers) {
            System.out.println(num);
        }

    }
}
发表于 2025-05-21 21:40:03 回复(0)
掌握java常用的api即可
发表于 2025-05-20 09:22:28 回复(0)
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n= in.nextInt();
        TreeSet<Integer> set= new TreeSet<Integer>();

        for(int i=0;i < n ;i++){
            int num=in.nextInt();
            set.add(num);
        }

        for(int num: set){
            System.out.println(num);
        }
    }  
}

发表于 2025-05-18 02:04:56 回复(0)
这个怎么知道终止输入了,也就是开始打印结果的时机
发表于 2025-05-01 16:52:38 回复(1)
import java.util.Scanner;
import java.util.ArrayList;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Integer n;
        ArrayList<Integer> arr0 = new ArrayList<Integer>();
        Integer iter1 = 0;

        n = in.nextInt();
        
        while (in.hasNextInt()) { 
            arr0.add(in.nextInt());
        }

        arr0.sort((a, b)->(a-b));
        for (iter1=0; iter1<n; ++iter1) {
            if ( iter1 > 0 && arr0.get(iter1) == arr0.get(iter1-1) ) {
                continue;
            }
            System.out.println(arr0.get(iter1));
        }
    }
}
来自java菜鸟的笨拙实现
发表于 2025-04-01 22:36:39 回复(0)
数组重新赋值,缺点就是这道题数据长度是500。
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int num = in.nextInt();
        int[] nums = new int[500];

        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            nums[a - 1] = a;
        }
        for (int j : nums) {
            if (j > 0) {
                System.out.println(j);
            }
        }
    }
}
发表于 2025-03-28 10:52:52 回复(0)
哈哈,最近学了一个新的类PriorityQueue,然后输出的时候去重就可以了。
import java.util.Scanner;
import java.util.PriorityQueue;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int num = in.nextInt();
            int[] ints = new int[num];
            PriorityQueue<Integer> queue = new PriorityQueue<>();
            for (int i = 0; i < num ; i++) {
                queue.add(in.nextInt());
            }
            int last = -1;
            // 输出去重
            while (!queue.isEmpty()) {
                int ii = queue.poll();
                if (ii != last) {
                    last = ii;
                    System.out.println(ii);
                }
            }
        }
    }

}


发表于 2025-03-24 17:35:46 回复(0)
// java实现
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int total = in.nextInt();
        int[] nums = new int[501];
        while (total-- > 0) {
            int i = in.nextInt();
            if (nums[i] == 0) nums[i]++;
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                System.out.println(i);
            }
        }
        in.close();
    }
}
发表于 2025-03-24 16:10:14 回复(0)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //读取整数
        int n = in.nextInt();
        //建立布尔数组
        boolean[] exist = new boolean[501];
        //标记是否存在
        for (int i = 0; i < n; i++) {
            int num = in.nextInt();
            exist[num] = true; //标记给出的数字是否出现过
        }

        //遍历数组,按从小到大排序
        for (int j = 1; j <= 500; j++) {
            if (exist[j]) {
                System.out.println(j);
            }
        }

    }

}

发表于 2025-02-05 09:49:24 回复(0)
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
            int count = in.nextInt();
            Set<Integer> data = new HashSet<Integer>();
           
            for(int i=0;i<count;i++){
                data.add(in.nextInt());
            }
            int[] arr = new int[data.size()];
            Iterator it = data.iterator();
            int index = 0;
            while(it.hasNext()) {
                arr[index] = (int) it.next();
                index++;
            }
            quickSort(arr,0,arr.length-1);
            for(int i = 0;i<arr.length;i++) {
                System.out.println(arr[i]);
            }
        }
    }

    // 快速排序
    public static void quickSort(int[] arr, int Left, int Right) {
        if(Left > Right) {
            return;
        }
        int L = Left;
        int R = Right;
        while(L < R) {
            while(L < R && arr[L] <arr[Left] ) L++;
            while(L < R && arr[R] > arr[Left]) R--;
            if(L == R) {
                int temp = arr[Left];
                arr[Left] = arr[L];
                arr[L] = temp;
            }else {
                int temp = arr[L];
                arr[L] = arr[R];
                arr[R] = temp;
            }

            if(L < Right) {
                quickSort(arr,L+1,Right);
            }
            if(R > Left){
                quickSort(arr,Left,R-1);
            }
        }
    }

}
发表于 2024-11-16 10:57:11 回复(0)
import java.awt.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        HashSet<Integer> set = new HashSet<>();
        while(n-- != 0){
            int a = in.nextInt();
            set.add(a);
        }
        ArrayList<Integer> list = new ArrayList<>(set);
        Collections.sort(list);
        for(Integer i : list){
            System.out.println(i);
        }
        in.close();
    }
}

发表于 2024-11-09 17:54:50 回复(1)
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        int next = 0;
        while (scanner.hasNextInt()) {
            next = scanner.nextInt();
            if (!list.contains(next)) {
                list.add(next);
            }
        }
        Collections.sort(list);
        for (Integer integer : list) {
            System.out.println(integer);
        }
发表于 2024-11-08 10:56:51 回复(0)
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Set<Integer> set = new TreeSet<>();
        for (int i = 0; i < n; i++) {
            set.add(sc.nextInt());
        }
        for (Integer i : set) {
            System.out.println(i);
        }
        sc.close();
    }
}

发表于 2024-11-02 09:52:51 回复(0)
投机取巧
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
         Scanner in = new Scanner(System.in);
        while (in.hasNextInt()){
            int length = in.nextInt();
            int[] arr = new int[Math.max(length, 501)];
            for (int i = 0; i < length; i++) {
                int randomNum = in.nextInt();
                if (arr[randomNum] == randomNum)continue;
                arr[randomNum]=randomNum;
            }
            for (int i = 0; i < 501; i++) {
                if (arr[i]!=0) System.out.println(arr[i]);
            }
        }
    }
}

发表于 2024-10-18 21:23:04 回复(0)
import java.util.*;  //因为要用到集合,又怕麻烦,所以就把包全引入了

/*
因为要删除重复的数字,第一时间就想到了集合中的set,因为set中不存重复的数据。
*/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();//第一个数是确定有多少个数的
        Set<Integer> hashSet = new HashSet<>(a);//所以可以将的到的第一个数作为要创建的Set的大小
        if(a > 0){
            while(in.hasNextInt()){   //如果还有数,则将他添加到set中
                int b = in.nextInt();
                hashSet.add(b);
            }
            List<Integer> list = new ArrayList<>(hashSet);//由于set是无序的,所以为了得到有序的内容,将他转换为list
            Collections.sort(list);//再利用collections的方法,将list进行排序
            Iterator iterator = list.iterator();//其实这里可以用foreach循环,只是上面用了hasNext,下意识就想到的迭代器
            while(iterator.hasNext()){
                System.out.println(iterator.next());//结果输出
            }
        }
    }
}

发表于 2024-10-10 19:44:02 回复(0)
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        int num = scanner.nextInt();
        for (int i = 0; i < num; i++) {
            list.add(scanner.nextInt());
        }
        list.stream().distinct().sorted().forEach(System.out::println);
        }
}
发表于 2024-09-13 15:09:11 回复(0)
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        TreeSet<Integer> mySet = new TreeSet<>(); //用TreeSet保证唯一有序
        int throuhOut = in.nextInt();// 第一行没用,扔掉
        while (in.hasNextInt()) { // 所有行加入TreeSet
            mySet.add(in.nextInt());
        }
        for (Integer obj : mySet){
            System.out.println(obj);
        }
    }
}

发表于 2024-09-10 00:03:46 回复(0)