[SDUT](1130)数据结构上机测试1:顺序表的应用

数据结构上机测试1:顺序表的应用

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
在长度为n(n<1000)的顺序表中可能存在着一些值相同的“多余”数据元素(类型为整型),编写一个程序将“多余”的数据元素从顺序表中删除,使该表由一个“非纯表”(值相同的元素在表中可能有多个)变成一个“纯表”(值相同的元素在表中只能有一个)。
Input
第一行输入表的长度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 
06using namespace std;
07 
08#define maxsize 1010
09typedef int ElemType;
10typedef struct
11{
12    ElemType data[maxsize];
13    int length;
14}sq;
15 
16void initlist(sq *L)
17{
18    L=(sq *)malloc(sizeof(sq));
19    L->length=0;
20}
21 
22void 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 
29void 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 
46void 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}
54int 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值