启程~我集训ac的第一道题~~数据结构上机测试1:顺序表应用

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

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic Discuss

Problem Description

在长度为n(n<1000)的顺序表中可能存在着一些值相同的“多余”数据元素(类型为整型),编写一个程序将“多余”的数据元素从顺序表中删除,使该表由一个“非纯表”(值相同的元素在表中可能有多个)变成一个“纯表”(值相同的元素在表中只能有一个)。

Input

第一行输入表的长度n;
第二行依次输入顺序表初始存放的n个元素值。

Output

第一行输出完成多余元素删除以后顺序表的元素个数;
第二行依次输出完成删除后的顺序表元素。

Sample Input

12
5 2 5 3 3 4 2 5 7 5 4 3

Sample Output

5
5 2 3 4 7

Hint

用尽可能少的时间和辅助存储空间。

Source

看到好多人都用链表做的,我就用数组做了一个....问题还有不少,比如最开始要求输出的“5 2 3 4 7”,而我的就会输出成“5 5 2 3 4 7”,一共有5个数,我的就输出成了6个......先放上最原始的代码~

#include <stdio.h>
#include<stdlib.h>
int main()
{
    int a[1005],b[1005], i, j,k,n,temp;
    scanf("%d",&n);
    for(i = 1; i <= n ; i++)
    {
        scanf("%d",&a[i]);
    }
    b[1] = a[1];
    k = 1;
    for(i = 1;i <= n;i++)
    {
        temp = 1;
        for(j = 0;j <= i-1;j++)
        {
            if(a[j] == a[i])
            {
                temp = 0;break;
            }
        }
        if(temp)
        {
            k++;
            b[k] = a[i];
        }
    }
printf("%d\n",k-1);
    for(i = 2; i <= k; i++)
    {
        if(i == 2)
            printf("%d",b[i]);
        else printf(" %d",b[i]);
    }
    printf("\n");
    return 0;
}

之后的改进版本~


#include <stdio.h>
#include<stdlib.h>
int main()
{
    int a[1005],b[1005], i, j,k,n,temp;
    scanf("%d",&n);
    for(i = 1; i <= n ; i++)
    {
        scanf("%d",&a[i]);//输入数组
    }
       b[1] = a[1];//用b数组来做 纯表
       k = 1;   //赋值 k来做b数组的第几个数的下标 同时到了后面可以直接利用k来表示有多少个元素
        for(i = 1; i <= n; i++)
    {
        temp = 1;//flag~旗帜
        for(j = 0; j <= i-1; j++)
        {
            if(a[j] == a[i])//用后面的数跟前面的数作比较,来判断是否是重复的
            {
                temp = 0;
                break;
            }
        }
        if(temp)//因为k++的缘故,输出完最后一个b值的时候k会多加1
        {

            b[k] = a[i];
            k++;
        }
    }
    printf("%d\n",k-1);//把多余的k删掉
    for(i = 1; i < k; i++)
    {
        if(i == 1)
            printf("%d",b[i]);
        else printf(" %d",b[i]);
    }
    printf("\n");
    return 0;
}

ps:关于链表的做法有机会我会补上的~可以参考链表训练中的2122题,应该是相同的。

好了,搞定了,下面附上2122题的代码。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n, i;
    struct node *head,*p,*q,*t;
    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;
    scanf("%d",&n);
    for(i = 1;i <= n;i++)
    {
       p = (struct node *)malloc(sizeof(struct node));
       scanf("%d",&p->data);
       p->next = head-> next;
       head -> next = p;
    }
    printf("%d\n",n);

    for(i = 1;i <= n;i++)
    {
        if(i == 1)
        {
            printf("%d",p->data);
        }
        else
        {
            printf(" %d",p->data);
        }
        p = p->next;
    }
    printf("\n");
    q =head->next;
   while(q)
   {
       t = q;
       p = q->next;
       while(p)
       {
           if(q->data == p->data)
           {
               t->next = p->next;
               n--;p=p->next;
           }
           else
           {
               t = t -> next;
               p = p->next;
           }
       }
       q = q->next;
   }
     printf("%d\n",n);
     p = head->next;
    for(i = 1;i <= n;i++)
    {
        if(i == 1)
        {
            printf("%d",p->data);
        }
        else
        {
            printf(" %d",p->data);
        }
        p = p->next;
    }
    printf("\n");
    return 0;
}

发上该题代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{
    int n, i;
    struct node *head ,*tail,*p,*t;
    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;
    tail = head;
    scanf("%d",&n);
    for(i =1;i<= n;i++)
    {
        p = (struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next = NULL;        //顺序建立链表
        tail ->next = p;
        tail = p;
    }
    tail = head->next;
    while(tail)
    {                
        t = tail;
        p = tail -> next; //用指针t和指针q来做比较,t负责删除重复数,主要是tail和q来比较data
        while(p)
        {
            if(tail->data == p->data)
            {
                t->next = p->next;   //如果tail的值与p的值相同,用t来完成删除,同时用n--来表示少了一个元素
                n--;
                p = p->next;
            }
            else
            {
                t= t->next;
                p = p->next;  //如果不相同,那么t和p都前移
            }

        }
         tail = tail->next; //比较完第一个数后 来比较下一个数
    }
    tail = head->next;
    printf("%d\n",n);
    for(i = 1;i<= n;i++)
    {
        if(i == 1)
        {
            printf("%d",tail->data);
        }
        else
            printf(" %d",tail->data); //输出
        tail = tail->next;
    }
    printf("\n");
    return 0;
}

就酱~

猜你喜欢

转载自blog.csdn.net/weixin_40616644/article/details/81286470