Http://acm.hrbust.edu.cn/index.php? M = problemset & A = showproblem & problem_id = 1545.
| Basic Data Structure-sequence table (2) |
| Time Limit: 1000 MS |
Memory limit: 10240 K |
| Total submit: 412 (165 Users) |
Total accepted: 188 (150 Users) |
Rating: |
Special Judge: No |
|
| Description |
In an ordered table with a length of n (n <1000), there may be "redundant" data elements with the same value (type: integer ), write a program to delete "redundant" data elements from the ordered table, so that the table is composed of a "non-pure table" (elements with the same value may have multiple) to a "Pure table" (only one element with the same value can exist in the table ). |
| Input |
The length of the input table in the first row is N; In the second row, enter the N element values initially stored in the sequence table. |
| Output |
The number of elements in the sequence table after the redundant elements are deleted in the first row; The second row outputs the sequence table elements after deletion. |
| Sample Input |
12 5 2 5 3 3 4 2 5 7 5 4 3 |
| Sample output |
5 5 2 3 4 7 |
Solution: Use a flag array to mark whether the current number has appeared after it
# Include <stdio. h>
Int A [1010];
Int flag [1010];
Int ans [1010];
Int main (){
Int N, I, j;
While (scanf ("% d", & N )! = EOF ){
For (I = 0; I <n; I ++ ){
Scanf ("% d", & A [I]);
Flag [I] = 0;
}
For (I = 0; I <n-1; I ++ ){
For (j = I + 1; j <n; j ++ ){
If (A [I] = A [J]) {
Flag [J] = 1;
}
}
}
For (I = J = 0; I <n; I ++ ){
If (flag [I] = 0 ){
Ans [J] = A [I];
J ++;
}
}
Printf ("% d \ n", J );
For (I = 0; I <j-1; I ++ ){
Printf ("% d", ANS [I]);
}
Printf ("% d \ n", ANS [I]);
}
Return 0;
} View code
Hrbust-1545-Basic Data Structure-sequence table (2)