Explicit random number, random number
/P
Problem descriptionI want to ask some students to do a questionnaire survey in school. In order to make the experiment objective, he first generates N random integers (N ≤ 1000) from 1 to 100 on the computer ), only one number is retained, and the remaining identical numbers are removed. Different numbers correspond to different student IDs. Then sort these numbers from small to large and sort them to the students for investigation. Please help clearly complete "de-duplication" and "sorting.
Input FormatThere are two rows in the input, and 1st act as a positive integer, indicating the number of random numbers generated:
N
The first row contains N positive integers separated by spaces, which are the random numbers generated.
Output FormatThe output is also two rows. 1st acts as a positive integer M, indicating the number of random numbers that are not the same. 2nd act M positive integers separated by spaces. They are random numbers in ascending order.
Sample Input10
20 40 32 67 40 20 89 300 400 15
Sample output8
15 20 32 40 67 89 300 400 solution: This question can be used to Input random numbers and sort them while ensuring that all numbers are not repeated.
# Include <iostream> using namespace std; int a [100]; int find (int x, int num) {// find the position where x can be inserted and return this location, if // not found (there are equal numbers) return-1 if (x> a [num-1]) return I; else if (x <a [0]) return 0; else {for (int j = 0; j <I-1; j ++) {if (x <a [j + 1] & x> a [j]) return j + 1;} return-1;} void insert (int x, int num, int pos) {// insert this value to the position of a [pos] for (int j = num-1; j> = pos; j --) a [j + 1] = a [j]; a [pos] = x;} int main () {int n, x, Pos; while (cin> n) {// n is the total random number. num records the number of random numbers that are not repeated, at least 1 int num = 1; for (int I = 0; I <n; I ++) {cin> x; if (I = 0) a [0] = x; else {pos = find (x, num); if (pos! =-1) {insert (x, num, pos); num ++ ;}}} cout <num <endl; for (int I = 0; I <num; I ++) cout <a [I] <""; cout <endl ;}}