標籤:asn class iterator div highlight ali 描述 rgs java
輸入描述:
輸入多行,先輸入隨機整數的個數,再輸入相應個數的整數
輸出描述:
返回多行,處理後的結果
輸入例子1:
11102040326740208930040015
輸出例子1:
10152032406789300400
分別使用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);}}}}
去重排序