数据结构上机测试1:顺序表的应用
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
在长度为n(n<1000)的顺序表中可能存在着一些值相同的“多余”数据元素(类型为整型),编写一个程序将“多余”的数据元素从顺序表中删除,使该表由一个“非纯表”(值相同的元素在表中可能有多个)变成一个“纯表”(值相同的元素在表中只能有一个)。
Input
第一行输入表的长度n;
第二行依次输入顺序表初始存放的n个元素值。
第二行依次输入顺序表初始存放的n个元素值。
Output
第一行输出完成多余元素删除以后顺序表的元素个数;
第二行依次输出完成删除后的顺序表元素。
第二行依次输出完成删除后的顺序表元素。
Example Input
12 5 2 5 3 3 4 2 5 7 5 4 3
Example Output
5 5 2 3 4 7
Hint
用尽可能少的时间和辅助存储空间。
Author
AC代码:
01 | #include<iostream> |
02 | #include<cstdio> |
03 | #include<cstdlib> |
04 | #include<cstring> |
05 |
06 | using namespace std; |
07 |
08 | #define maxsize 1010 |
09 | typedef int ElemType; |
10 | typedef struct |
11 | { |
12 | ElemType data[maxsize]; |
13 | int length; |
14 | }sq; |
15 |
16 | void initlist(sq *L) |
17 | { |
18 | L=(sq *) malloc ( sizeof (sq)); |
19 | L->length=0; |
20 | } |
21 |
22 | void creat(sq * L, int n) |
23 | { |
24 | for ( int i = 0; i < n; i++) |
25 | cin >> L->data[i]; |
26 | L->length = n; |
27 | } |
28 |
29 | void del(sq * L) |
30 | { |
31 | for ( int i = 0; i < L->length; i++) |
32 | { |
33 | for ( int j = i + 1; j < L->length; j++) |
34 | { |
35 | if (L->data[i] == L->data[j]) |
36 | { |
37 | for ( int k = j; k < L->length - 1; k++) |
38 | L->data[k] = L->data[k + 1]; |
39 | L->length--; |
40 | j--; |
41 | } |
42 | } |
43 |
}
|
44 | } |
45 |
46 | void show(sq * L) |
47 | { |
48 | cout << L->length << endl; |
49 | for ( int i = 0; i < L->length - 1; i++) |
50 | cout << L->data[i] << " " ; |
51 | cout << L->data[L->length - 1] << endl; |
52 |
53 | } |
54 | int main() |
55 | { |
56 | sq L; |
57 | int n; |
58 | cin >> n; |
59 | initlist(&L); |
60 | creat(&L, n); |
61 | del(&L); |
62 | show(&L); |
63 | return 0; |
64 |
}
|
http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/1130.html