Description
The "C Xiaojia" team of the ACM team wants to invite some students to conduct a questionnaire survey in the school to ensure the objectivity of the experiment, he first generates N random integers (0 <N ≤ 1000) between 1 and 100 on the computer. For repeated numbers, only one 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 C Xiaojia complete "de-duplication" and "sorting.
-
Input
-
The number of groups of test data in the first line is represented by an integer T (1 <T <10,
Each group of test data includes two rows,
1st Act 1 positive integer, indicating the number of random numbers generated: N (0 <N ≤ 100)
The first row contains N positive integers separated by spaces, which are the random numbers generated.
(The random number is given by the question and does not need to be generated by ACMer)
-
Output
-
The 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 input
-
11020 40 32 67 40 20 89 300 400 15
-
Sample output
-
815 20 32 40 67 89 300 400
Solution:
This question is divided into two parts: Sorting and deduplication.
Quick sorting is the best option when there are too many data. The program code of quick sorting is as follows:
Int cmp (const void * a, const void * B) {return (* (int *) a-* (int *) B);} qsort (a, m, sizeof (a [0]), cmp );
There are multiple deduplication methods. The method I chose is to overwrite the same data to achieve deduplication.
Program code:
# Include <stdio. h> # include <stdlib. h> // void fun (int a [], int n); int cmp (const void * a, const void * B) {return (* (int *) a-* (int *) B);} int main () {int n, m, a [105], I, j, count = 0; scanf ("% d", & n); while (n --) {scanf ("% d", & m); // getchar (); for (I = 0; I <m; I ++) {scanf ("% d", & a [I]) ;}// fun (a, m); qsort (a, m, sizeof (a [0]), cmp); for (I = 0; I <m; I ++) {if (a [I] = a [i-1]) {for (j = I; j <M-1; j ++) a [j] = a [j + 1]; m --; I --;}} printf ("% d \ n", m); for (I = 0; I <m; I ++) printf ("% d", a [I]); printf ("\ n");} return 0 ;}
C small plus random number