zoukankan      html  css  js  c++  java
  • 顺序表应用2:多余元素删除之建表算法(SDUT 3325)

    题解: 每次询问一遍,如果已经存在就不用插入表中了。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct node
    {
        int *elem;
        int len;
    };
    
    void CreatList(int m, struct node &list)
    {
        list.elem = (int *)malloc(101 * sizeof(int));
        list.len = 0;
        int x,f = 1;
        for(int i = 0; i < m; i ++)
        {
            f = 1;
            scanf("%d", &x);
            if(i == 0)
                list.elem[list.len ++] = x;
            else
            {
                for(int j = 0; j  < list.len; j ++)
                {
                    if(list.elem[j] == x)
                    {
                        f = 0;
                        break;
                    }
                }
                if(f)
                    list.elem[list.len ++] = x;
            }
        }
    }
    
    void print(struct node &list)
    {
        for(int i = 0; i < list.len; i ++)
        {
            if(i == 0)
                printf("%d",list.elem[i]);
            else
                printf(" %d", list.elem[i]);
        }
        printf("
    ");
    }
    int main()
    {
        int n, m;
        scanf("%d",&n);
        while(n --)
        {
            struct node list;
            scanf("%d",&m);
            CreatList(m,list);
            print(list);
        }
        return 0;
    }
    

    Problem Description

    一个长度不超过10000数据的顺序表,可能存在着一些值相同的“多余”数据元素(类型为整型),编写一个程序将“多余”的数据元素从顺序表中删除,使该表由一个“非纯表”(值相同的元素在表中可能有多个)变成一个“纯表”(值相同的元素在表中只保留第一个)。
    要求:
           1、必须先定义线性表的结构与操作函数,在主函数中借助该定义与操作函数调用实现问题功能;
          2、本题的目标是熟悉在顺序表原表空间基础上建新表的算法,要在原顺序表空间的基础上完成完成删除,建表过程不得开辟新的表空间;

    3、不得采用原表元素移位删除的方式。


    Input

     第一行输入整数n,代表下面有n行输入;
    之后输入n行,每行先输入整数m,之后输入m个数据,代表对应顺序表的每个元素。


    Output

     输出有n行,为每个顺序表删除多余元素后的结果


    Sample Input

    4
    5 6 9 6 8 9
    3 5 5 5
    5 9 8 7 6 5
    10 1 2 3 4 5 5 4 2 1 3


    Sample Output

    6 9 8
    5
    9 8 7 6 5
    1 2 3 4 5

  • 相关阅读:
    Python 猜数小程序(练习)
    Mysql 字符串日期互转
    MaxCompute 语句笔记
    数据仓库架构
    Python 比较两个字符串的相似度
    Python print
    Python简单计算器
    HashMap为什么线程不安全(死循环+数据丢失过程分析)
    浅谈ArrayList、Vector和LinkedList
    JAVA对象的浅克隆和深克隆
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139561.html
Copyright © 2011-2022 走看看