publicclassMain{ publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); List<Integer> list = new ArrayList<Integer>(); sc.nextInt(); while (sc.hasNextInt()) list.add(sc.nextInt()); for (int i = 0; i < list.size() - 1; i++) for (int j = list.size() - 1; j > i; j--) if (list.get(j).equals(list.get(i))) list.remove(j); Integer[] num = list.toArray(new Integer[0]); Arrays.sort(num); System.out.println(list.size()); for (int i : num) System.out.print(i + " "); } }
C++实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<cstdio> int a[1001]; intmain(){ int i, n, t; int count = 0; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &t); if (a[t] == 0) { a[t] = 1; count++; } } printf("%d\n", count); for (i = 1; i <= 1000; i++) if (a[i] == 1) printf("%d ", i); }