题解 | #明明的随机数#
明明的随机数
http://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
利用HashSet去重,然后利用List存储元素集合,然后利用Collections进行排序
import java.util.*; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int n = scanner.nextInt(); Set<Integer> set = new HashSet<Integer>(); for(int i = 0; i < n; i++){ int t = scanner.nextInt(); set.add(t); } List<Integer>list = new ArrayList<>(set); Collections.sort(list); for(int i = 0; i < list.size(); i++){ System.out.println(list.get(i)); } } } }
或者利用TreeSet进行数据处理 然后直接输出
import java.util.*; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ int n = scanner.nextInt(); TreeSet<Integer> set = new TreeSet<>(); for (int i = 0; i < n; i++){ set.add(scanner.nextInt()); } for (Integer integer : set) { System.out.println(integer); } } } }