6-14 Duplicated Numbers (10分)
This program reads a lot of integers, in which may be duplicated numbers. The program picks out all the duplicated ones and sorts the remainders in a descendent order.
函數接口定義:
public static ArrayList<Integer> pick(ArrayList<Integer> a);
a
is the ArrayList
to be parsed and returns the result as an ArrayList
.
裁判測試程序樣例:
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) {
ArrayList<Integer> lst = new ArrayList<>();
Scanner in = new Scanner(System.in);
while ( in.hasNextInt() ) {
lst.add(in.nextInt());
}
lst = pick(lst);
for ( int x:lst) {
System.out.print(x+" ");
}
System.out.println();
in.close();
}
}
輸入樣例:
1 1 2 3 4 5 9 3
輸出樣例:
9 5 4 3 2 1
public static ArrayList<Integer> pick(ArrayList<Integer> a) {
ArrayList<Integer> rs = new ArrayList<Integer>();
for(int i=0;i<a.size();i++) {
if(!rs.contains(a.get(i))) {
rs.add(a.get(i));
}
}
Collections.sort(rs);
Collections.reverse(rs);
return rs;
}