输入描述:
输入多行,先输入随机整数的个数,再输入相应个数的整数
输出描述:
返回多行,处理后的结果
输入例子1:
11 10 20 40 32 67 40 20 89 300 400 15
输出例子1:
10 15 20 32 40 67 89 300 400
分别使用HashSet与TreeSet实现
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; public class M2 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); while (scanner.hasNext()) { HashSet<Integer> aList=new HashSet<Integer>(); int i=scanner.nextInt(); for (int j=0; j< i; j++) { aList.add(scanner.nextInt()); } Iterator iterator=aList.iterator(); ArrayList<Integer> n =new ArrayList<Integer>(); for (int j = 0; j < aList.size(); j++) { n.add((Integer) iterator.next()); } Collections.sort(n); for (int m = 0; m < aList.size(); m++) { System.out.println(n.get(m)); } } } }
while(sc.hasNext())不只一个输入
import java.util.Scanner; import java.util.TreeSet; public class M3 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); while (scanner.hasNext()) { TreeSet<Integer> aList=new TreeSet<Integer>(); int i=scanner.nextInt(); for (int j=0; j< i; j++) { aList.add(scanner.nextInt()); } for (Integer m : aList) { System.out.println(m); } } } }