1045: 集合的交运算的实现

题目描述
假设有两个集合 A 和 B 分别用两个线性表 LA 和 LB 表示,即线性表中的数据元素即为集合中的成员。编程实现集合A和集合B的交运算。

输入
第一行为集合A的数据元素个数n;
第二行输入n个集合A的数据元素 ;
第三行为集合B的数据元素的个数;
第四行输入m个集合B的数据元素

输出
A和B的交集

样例输入
8
0 5 6 3 8 7 9 10
7
1 3 4 7 8 9 5

样例输出
5 3 8 7 9

#include<stdio.h>
#include<malloc.h>
#define MaxSize 1000
typedef struct node
{
    int data[MaxSize];
    int length;
}Lnode;
void init(Lnode *&L)
{
    L=(Lnode*)malloc(sizeof(Lnode));
    L->length=0;
}
void Insert(Lnode*&L,int num)
{
    L->data[L->length++]=num;
}
int main()
{
    Lnode *L1,*L2;
    init(L1);
    init(L2);
    int n,num;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&num);
        Insert(L1,num);
    }
    int m;
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d",&num);
        Insert(L2,num);
    }
    for(int i=0;i<L1->length;i++)
        //遍历L1中的每个数,在L2中查找,如同找到相同数则输出
    {
        for(int j=0;j<L2->length;j++)
        {
            if(L1->data[i]==L2->data[j])
            {
                printf("%d ",L1->data[i]);
            }
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值