import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int inputSize = in.nextInt(); short[] sortRes = new short[500]; while (in.hasNextInt()) { // 注意 while 处理多个 case int nextInput = in.nextInt(); if (sortRes[nextInput] == 0) { sortRes[nextInput] = 1; } } for (int i = 0;i<sortRes.length;i++) { if (sortRes[i] == 1) { System.out.println(i); } } } }
用数组下标记录出现过的数,先初始化一个size为500的数组,因为题目条件是出现的随机数取值范围在500以下,然后对于每一个输入的随机数,只要对应下标处为0,就说明之前没有出现过,那么就设成1,表示已经出现过了
最后遍历数组,只要该下标处为1,就说明出现过,那么就将下标输出,表示这个数出现过,就可以了