首页 > 试题广场 >

明明的随机数

[编程题]明明的随机数
  • 热度指数:1680306 时间限制: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.TreeSet;
import java.util.Set;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt(); // 读取数字个数
        int[] intArr = new int[n] ;
        Set<Integer> set = new TreeSet<>();
        for (int i = 0;i<n;i++){
             set.add(in.nextInt());
        }
         for (int num : set) {
            System.out.println(num);
        }
    }
}


发表于 2025-09-18 16:52:04 回复(0)
import java.util.*; 
import java.io.*;

// 用包的方法,红黑树
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf=new  BufferedReader(new InputStreamReader(System.in));
        int n=Integer.parseInt(bf.readLine());
        //treeset创建时定义排序规则比较器
        TreeSet<Integer> ts=new TreeSet<>((o1,o2)->o1-o2);
        for(int i=0;i<n;i++){
            int elem=Integer.parseInt(bf.readLine());
            ts.add(elem);
        }
        ts.forEach(System.out::println);//打印结果
    }
}

发表于 2025-09-13 13:31:58 回复(0)
import java.util.*;
import java.util.stream.*;

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

        while (in.hasNextInt()) {
            int i = in.nextInt();
            set.add(i);
        }
        set.stream().sorted().forEach(System.out::println);
    }

}

发表于 2025-09-09 16:37:27 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<n;i++){
            int num=in.nextInt();
            if(!list.contains(num)){
                list.add(num);
            }
        }
        Collections.sort(list);
        for(int num:list){
            System.out.println(num);
        }
        // for(int i=0;i<list.size();i++){
        //     System.out.println(list.get(i));
        // }
    }
}
发表于 2025-09-08 22:10:57 回复(0)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean []arr = new boolean[505];
        int num = sc.nextInt();
        while(num -- > 0){
            int input = sc.nextInt();
            arr[input] = true;
        }
        for(int i = 0;i < 500; ++i){
            if(arr[i]) System.out.println(i);
        }
    }
}
发表于 2025-09-03 16:16:45 回复(0)
用HashSet不重复的存储数字,把它转换为数组,对数组从小到大排序,依次输出数组里的数据即可。
import java.util.Scanner;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            set.add(in.nextInt());
        }
        int[] res = new int[set.size()];
        int index = 0;
        for (int temp : set) {
            res[index++] = temp;
        }
        Arrays.sort(res);
        for (int temp : res) {
            System.out.println(temp);
        }
    }
}

发表于 2025-07-20 00:52:18 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int i = in.nextInt();
        TreeSet<Integer> set = new TreeSet<>();
        while(i> 0){
            set.add(in.nextInt());
            i--;
        }
        for(Integer s: set){
            System.out.println(s);
        }
    }
}
发表于 2025-07-11 16:29:43 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(); // 读取整数个数
        Set<Integer> set = new TreeSet<>(); // 直接使用TreeSet存储(自动排序+去重)
       
        for (int i = 0; i < n; i++) {
            set.add(sc.nextInt()); // 添加元素,TreeSet会自动排序和去重
        }
       
        for (int num : set) {
            System.out.println(num); // 输出排序且去重后的结果
        }
    }
}
发表于 2025-07-10 01:56:37 回复(0)
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 回复(1)
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)

问题信息

难度:
429条回答 295388浏览

热门推荐

通过挑战的用户

查看代码
明明的随机数