華爲機試3

題目描述

明明想在學校中請一些同學一起做一項問卷調查,爲了實驗的客觀性,他先用計算機生成了N個1到1000之間的隨機整數(N≤1000),對於其中重複的數字,只保留一個,把其餘相同的數去掉,不同的數對應着不同的學生的學號。然後再把這些數從小到大排序,按照排好的順序去找同學做調查。請你協助明明完成“去重”與“排序”的工作(同一個測試用例裏可能會有多組數據,希望大家能正確處理)。

Input Param

n               輸入隨機數的個數

inputArray      n個隨機整數組成的數組

Return Value

OutputArray    輸出處理後的隨機整數
注:測試用例保證輸入參數的正確性,答題者無需驗證。測試用例不止一組。

樣例輸入解釋:

樣例有兩組測試

第一組是3個數字,分別是:2,2,1。

第二組是11個數字,分別是:10,20,40,32,67,40,20,89,300,400,15。

輸入描述:

輸入多行,先輸入隨機整數的個數,再輸入相應個數的整數

輸出描述:

返回多行,處理後的結果

示例1

輸入

3
2
2
1
11
10
20
40
32
67
40
20
89
300
400
15

輸出

1
2
10
15
20
32
40
67
89
300
400
public static void main(String[] agrs){
            Scanner sc = new Scanner(System.in);
            while(sc.hasNext()){
                int n = sc.nextInt();
                int[] temp = new int [n];
                for(int i=0;i<n;++i){
                    temp[i]=sc.nextInt();
                }
                Arrays.sort(temp);
                ArrayList list = new ArrayList();
                list.add(temp[0]);
                for(int i =1;i<n;++i){
                    if(temp[i-1]==temp[i]){

                    }else
                        list.add(temp[i]);
                }
                for(Object j:list){
                    System.out.println(j);
                }
            }
        }
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //獲取個數
        int num = sc.nextInt();
        //創建TreeSet進行去重排序
        TreeSet set = new TreeSet();
        //輸入
        for(int i =0 ; i < num ;i++){
            set.add(sc.nextInt());
        }

        //輸出
        Iterator iterator = set.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章