明明想在學校中請一些同學一起做一項問卷調查,為了實驗的客觀性,他先用計算機生成了N個1到1000之間的隨機整數(N≤1000),對於其中重復的數字,只保留一個,把其余相同的數去掉,不同的數對應着不同的學生的學號。然后再把這些數從小到大排序,按照排好的順序去找同學做調查。請你協助明明完成“去重”與“排序”的工作。
Input Param
n 輸入隨機數的個數
inputArray n個隨機整數組成的數組
Return Value
OutputArray 輸出處理后的隨機整數
注:測試用例保證輸入參數的正確性,答題者無需驗證。測試用例不止一組。
輸入描述:
輸入多行,先輸入隨機整數的個數,再輸入相應個數的整數
輸出描述:
返回多行,處理后的結果
輸入例子:
11 10 20 40 32 67 40 20 89 300 400 15
輸出例子:
10 15 20 32 40 67 89 300 400
//思路:形成一個1000的數組,然后置零,然后讀取第一個數字 num 是題目中的那個個數,然后再依次讀取下面的數字,每一個讀到的數字都要寫入數組 ,比如說讀到了100,則數組【100】就寫入100,最后再把非零的數字全部println出來。
import java.util.Scanner;
public class Main{
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
int[] target = new int[1000];
while (scan.hasNextInt()) {
for (int i = 0; i < 1000; i++) {
target[i] = 0;
}
int num = scan.nextInt();
for (int i = 0; i < num; i++) {
int next = scan.nextInt();
target[next] = next;
}
for (int i = 0; i < 1000; i++) {
if (target[i] == 0) {
} else {
System.out.println(target[i]);
}
}
}
}
}