第一行输入一个整数
,代表明明生成的数字个数。
此后
行,第
行输入一个整数
,代表明明生成的随机整数。
输出若干行,每行输出一个整数,代表输入数据排序后的结果。第一行输出最小的数字。
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); } }这个重点是考察对复杂数据类型的操作
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); } } }
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菜鸟的笨拙实现
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); } } } } }
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); } } } }
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(); } }
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); }
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(); } }
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());//结果输出 } } } }
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); } } }