describe
Obviously, N random integers between 1 and 500 are generated. Please delete the repeated numbers, that is, keep only one of the same numbers, remove the rest of the same numbers, and then sort these numbers from small to large, and output them in the order they are arranged.
Data range: 1≤n≤1000, the size of the input number satisfies 1≤val≤500
Input description: first input the number N of random integers in the first line. Enter an integer in each of the next N lines, representing a random number that is clearly generated.
Output description: Output multiple lines, indicating the result of input data processing
solution
This question examines the usage and sorting of the Set structure.
- The first line first inputs the number N of random integers.
- Enter an integer in each of the next N lines, representing a random number that is clearly generated.
- Random numbers are stored in the Set structure to achieve deduplication.
- The Set structure is converted to a List and sorted.
- Output the sorted results.
/*
* Copyright (c) waylau.com, 2022. All rights reserved.
*/
package com.waylau.nowcoder.exam.oj.huawei;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
/**
* HJ3 Mingming random number. Description: Mingming generates N random integers between 1 and 500. Please delete the repeated numbers,
* That is, keep only one of the same numbers, remove the rest of the same numbers, and then sort these numbers from small to large, and output them in the order they are arranged. Data range: 1≤n≤1000
* , the size of the input number satisfies 1≤val≤500 Input description: first input the number N of random integers in the first line. Enter an integer in each of the next N lines, representing a random number that is clearly generated.
* Output description: Output multiple lines, indicating the result of input data processing
*
* @author Way Lau
* @since 2022-08-05
*/
public class HJ3RandomNumbers {
public static void main(String[] args) {
// Enter a line, representing the number N of random integers.
Scanner sc = new Scanner(System.in);
// Convert all to lowercase
int num = sc.nextInt();
// Construct a Set structure to realize digital deduplication
Set<Integer> set = new HashSet<>();
// Traversing the input numbers, each time a number is traversed, the character is recorded in the Set
for (int i = 0; i < num; i++) {
int randomNumer = sc.nextInt();
set.add(randomNumer);
}
// The Set structure is converted to a List and sorted.
List<Integer> sortedNumbers = new ArrayList<Integer>(set);
sortedNumbers.sort(Comparator.naturalOrder());
// Iterate through each integer output
sortedNumbers.forEach(System.out::println);
// close resource
sc.close();
}
}
Solution 2
Deduplication and sorting, isn't this the data structure of TreeSet! Therefore, it can be solved directly with TreeSet.
- The first line first inputs the number N of random integers.
- Enter an integer in each of the next N lines, representing a random number that is clearly generated.
- Random numbers are stored in the TreeSet structure to achieve deduplication and sorting.
- Output the sorted results.
code show as below:
/*
* Copyright (c) waylau.com, 2022. All rights reserved.
*/
package com.waylau.nowcoder.exam.oj.huawei;
import java.util.Scanner;
import java.util.TreeSet;
/**
* HJ3 Mingming random number. Description: Mingming generates N random integers between 1 and 500. Please delete the repeated numbers,
* That is, keep only one of the same numbers, remove the rest of the same numbers, and then sort these numbers from small to large, and output them in the order they are arranged. Data range: 1≤n≤1000
* , the size of the input number satisfies 1≤val≤500 Input description: first input the number N of random integers in the first line. Enter an integer in each of the next N lines, representing a random number that is clearly generated.
* Output description: Output multiple lines, indicating the result of input data processing
*
* @author Way Lau
* @since 2022-08-05
*/
public class HJ3RandomNumbers2 {
public static void main(String[] args) {
// Enter a line, representing the number N of random integers.
Scanner sc = new Scanner(System.in);
// Convert all to lowercase
int num = sc.nextInt();
// Construct a TreeSet structure to realize deduplication and sorting of numbers
TreeSet<Integer> set = new TreeSet<>();
// Traversing the input numbers, each time a number is traversed, the character is recorded in the Set
for (int i = 0; i < num; i++) {
int randomNumer = sc.nextInt();
set.add(randomNumer);
}
// Iterate through each integer output
set.forEach(System.out::println);
// close resource
sc.close();
}
}
Run the program, the output is as follows:
3
2
2
1
1
2
References
- This series is archived at https://github.com/waylau/nowcoder-exam-oj
- "Java Data Structures and Algorithms in Action": https://github.com/waylau/java-data-structures-and-algorithms-in-action
- "Data Structure and Algorithm Fundamentals (Java Language Implementation)" (Liu Weiwei, published by Peking University Press): "Data Structure and Algorithm Fundamentals (Java Language Implementation)" (Liu Weiwei)【Abstract Book Review Trial Reading】- Jingdong Books