题解 | #明明的随机数#
明明的随机数
https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
test6(in);
}
}
public static void test1(Scanner in) {
int n = in.nextInt();
Set<Integer> set = new TreeSet<>();
for (int i = 0; i < n; i++) {
int a = in.nextInt();
set.add(a);
}
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
public static void test2(Scanner in) {
int n = in.nextInt();
int[] temp = new int[500];
for (int i = 0; i < n; i++) {
int a = in.nextInt();
temp[a - 1] = 1;
}
for (int i = 0; i < 500; i++) {
if (temp[i] == 1) {
System.out.println(i + 1);
}
}
}
public static void test3(Scanner in) {
int n = in.nextInt();
int[] temp = new int[n];
for (int i = 0; i < n; i++) {
int a = in.nextInt();
temp[i] = a;
}
Arrays.sort(temp);
System.out.println(temp[0]);
for (int i = 1; i < temp.length; i++) {
if (temp[i-1] != temp[i]) {
System.out.println(temp[i]);
}
}
}
public static void test4(Scanner in) {
int n = in.nextInt();
Set<Integer> set = new HashSet<>();
for (int i = 0; i < n; i++) {
int a = in.nextInt();
set.add(a);
}
Object[] temp = set.toArray();
Arrays.sort(temp);
for (int i = 0; i < temp.length; i++) {
System.out.println(temp[i]);
}
}
public static void test5(Scanner in) {
int n = in.nextInt();
int[] temp = new int[n];
for (int i = 0; i < n; i++) {
int a = in.nextInt();
temp[i] = a;
}
for (int i = 0;i<n;i++) {
for (int j = i+1;j<n;j++){
if(temp[i]>temp[j]){
int t = temp[i];
temp[i] = temp[j];
temp[j] = t;
} else if (temp[i] == temp[j]) {
temp[j] = 0;
}
}
}
for(int tem : temp) {
if (tem != 0) {
System.out.println(tem);
}
}
}
public static void test6(Scanner in) {
int n = in.nextInt();
Set<Integer> set = new HashSet<>();
for (int i = 0; i < n; i++) {
int a = in.nextInt();
set.add(a);
}
List<Integer> list = new ArrayList<Integer>(set);
// list.sort(Comparator.naturalOrder());
list.sort((o1,o2) -> o1 > o2 ? 1 : -1);
list.forEach(System.out::println);
}
public static void test7(Scanner in) {
}
}
查看21道真题和解析
