❶ c语言 判断两集合之间的关系
给你一个算法:
A,B两个集合,测试两个条件:
1、看看是否所有A的元素都能在B中找到;
2、看看是否所有B的元素都能在A中找到;
如果两个条件都满足: A equals B
如果只有第一个条件满足: A is proper subset of B
如果只有第二个条件满足:B is proper subset of A
如果两个条件都不满足:
这时候 你考虑他们是否有共同的元素
没有任何公共元素显示 A and B are disjoint
有公共元素显示 I'm confused
❷ 如何写一个c语言程序求两个集合的交集
定义两个数组存放这两个集合,再定义一个数组存放它们的集合,用类似冒泡排序的算法,遍历数组1中的第一个元素和数组2中每一个元素,若有相同的,则把这个元素放入第三个数组,继续遍历,知道数组1遍历完所有元素,那数组3中的元素,即为两个数组(集合)的交集。
❸ C语言求集合运算
可以用线性表模拟集合,把两个线性表中一样的数提取出来就是交集,所有元素组成的就是并集,还可以用C++重载运算符实现+就求并集之类的。
❹ 如何用C语言编写求交集和并集的程序
char c[20];//存储交集的字符int count=0;//统计交集个数for (n=1;n<j;n++)
for (m=1;m<=k;m++)
{
if(a[n]==b[m]) { c[count]=a[n]; count++; }
}
} c[count]='\0';printf("交集为%s",c);
❺ 怎样用C语言求集合A与几何B的复合关系
//说明:输入的格式需要提示按输入,因为要获取正确的有序对才能进行复合运算
/*
*************输入格式如:a b, #,# 退出***************
输入:a b
输入:b t
输入:t d
输入:s j
输入:j i
输入:c a
*/
#include "stdlib.h"
typedef char Element;
struct Node
{
Element left;
Element right;
struct Node *next;
};
struct Node *CreateLink();
struct Node *Operation(struct Node *R,struct Node *S);
void PrintLink(struct Node *h);
int main()
{
struct Node *hdR,*hdS,*rhd;
printf("请输入第一个集合R的关系\n");
hdR = CreateLink();
PrintLink(hdR);
printf("\n请输入第二个集合S的关系\n");
hdS = CreateLink();
PrintLink(hdS);
rhd = Operation(hdR,hdS);
if (rhd->next == NULL)
{
printf("\nR。S结果为空集\n");
}
else
{
printf("\nR。S结果为:\n");
PrintLink(rhd);
}
return 0;
}
struct Node *CreateLink()
{
struct Node *head, *p;
printf("*************输入格式如:a b, \'#,#\' 退出***************\n");
Element a,b;
head = (struct Node *)malloc(sizeof(struct Node));
head->left = 0;
head->right = 0;
head->next = NULL;
printf("输入:");
scanf("%c %c",&a,&b);
getchar();
while (a != '#')
{
p = (struct Node *)malloc(sizeof(struct Node));
p->left = a;
p->right = b;
p->next = head->next;
head->next = p;
printf("输入:");
scanf("%c %c",&a,&b);
getchar();
}
return head;
}
struct Node *Operation(struct Node *R, struct Node *S)
{
struct Node *newHead,*newP,*newQ;
struct Node *pH, *pNext;
newHead = (struct Node *)malloc(sizeof(struct Node));
newHead->left = 0;
newHead->right = 0;
newHead->next = NULL;
newP = newHead;
if (R->next == NULL || S->next == NULL)
{
return newP;
}
char cLeft,cRight;
pH = R;
while (pH->next != NULL)
{
cLeft = pH->next->left;
cRight = pH->next->right;
pNext = S->next;
while(pNext != NULL)
{
//存在可以复合运算的
if (cRight == pNext->left)
{
//在复合运算结果集中插入数据,如果存在相同的二元关系,则不需要插入
newP = newHead;
while (newP->next != NULL)
{
if (cLeft == newP->left && cRight == newP->right)
{
break;
}
newP = newP->next;
}
if (newP->next == NULL)
{
newQ = (struct Node *)malloc(sizeof(struct Node));
newQ->left = cLeft;
newQ->right = pNext->right;
newQ->next = NULL;
newP->next = newQ;
// newQ->next = newP->next->next;
}
}
pNext = pNext->next;
}
pH = pH->next;
}
return newHead;
}
void PrintLink(struct Node *h)
{
struct Node *p=h->next;
printf("\n");
while (p != NULL)
{
printf("<%c,%c> ",p->left,p->right);
p = p->next;
}
printf("\n");
}
❻ 用c语言编写一段程序,求两个集合的交
/*链表实现集合运算*/
#include<iostream.h>
typedef int ElemType;
struct SNode {
ElemType data;
SNode* next;
};
void InitSet(SNode*& HT)
{
HT=NULL;
}
void ClearSet(SNode*& HT)
{
SNode *p=HT, *q;
while(p!=NULL)
{
q=p->next;
delete p;
p=q;
}
HT=NULL;
}
int LenthSet(SNode* HT)
{
int n=0;
while(HT!=NULL)
{
n++;
HT=HT->next;
}
return n;
}
bool EmptySet(SNode* HT)
{
return HT==NULL;
}
bool Inset(SNode* HT, ElemType item)
{
while(HT!=NULL)
{
if(HT->data==item) return true;
else HT=HT->next;
}
return false;
}
void OutputSet(SNode* HT)
{
while(HT!=NULL)
{
cout<<HT->data<<' ';
HT=HT->next;
}
cout<<endl;
}
bool FindSet(SNode* HT, ElemType& item)
{
while(HT!=NULL)
{
if(HT->data==item) break;
else HT=HT->next;
}
if(HT!=NULL)
{
item=HT->data;
return true;
}
else return false;
}
bool ModifySet(SNode* HT, ElemType item, ElemType temp)
{
while(HT!=NULL)
{
if(HT->data==item) break;
else HT=HT->next;
}
if(HT!=NULL)
{
HT->data=temp;
return true;
}
else return false;
}
bool InsertSet(SNode*& HT, ElemType item)
{
//建立值为item的新结点
SNode* newptr= new SNode;
newptr->data=item;
//从单链表中顺序查找是否存在值为item的结点
SNode* p=HT;
while(p!=NULL)
{
if(p->data==item) break;
else p=p->next;
}
// 若不存在则把新结点插入到表头并返回真,否则不返回假
if(p==NULL)
{
newptr->next=HT;
HT=newptr;
return true;
}
else return false;
}
bool DeleteSet(SNode*& HT, ElemType& item)
{
//从单链表中顺序查找是否存在值为item的结点
SNode *cp=HT, *ap=NULL;
while(cp!=NULL)
{
if(cp->data==item) break;
else
{
ap=cp;
cp=cp->next;
}
}
//若不存在则不返回假,表明删除失败
if(cp==NULL)
return false;
//由item带回待删除结点cp的完整值,若不需要带回可设置item为值参
item=cp->data;
if(ap==NULL) HT=cp->next;
//从单链表中删除已经找到的cp结点并对ap是否为表头做不同处理
else ap->next = cp->next;
//删除cp结点后返回真
delete cp;
return true;
}
//求两集合的并集
void UnionSet(SNode* HT1, SNode* HT2, SNode*& HT)
{
HT=NULL;
//把HT1集合单链表元素复制到HT集合单链表中
SNode* p=HT1;
while(p!=NULL)
{
//建立新结点并赋值为p->data
SNode* newptr=new SNode;
newptr->data=p->data;
//把新结点插入到HT集合单链表的表头并让P指向下一个结点
newptr->next=HT;
HT=newptr;
p=p->next;
}
//把HT1集合单链表的每个元素插入到HT集合单链表中
p=HT2;
while(p!=NULL)
{
InsertSet(HT, p->data);
p=p->next;
}
}
//求集合的交集
void InterseSet(SNode* HT1, SNode* HT2, SNode*& HT)
{
HT=NULL;
ElemType x;
SNode* p=HT2;
while(p!=NULL)
{
x=p->data;
bool b=FindSet(HT1,x); //用x查找HT1集合
if(b) InsertSet(HT,x); //若查找成功则把x插入到HT集合中
p=p->next;
}
if(HT==NULL)
cout<<"空集"<<endl;
}
//求集合的差集
void DifferenceSet(SNode* HT1, SNode* HT2, SNode*& HT)
{
HT=NULL;
ElemType x;
SNode* p=HT1;
while(p!=NULL)
{
x=p->data;
bool b=FindSet(HT2,x); //用x查找HT2集合
if(b) InsertSet(HT,x); //若查找失败则把x插入到HT集合中
p=p->next;
}
if(HT==NULL)
cout<<"空集"<<endl;
}
int main()
{
SNode *a,*b,*c,*d;
InitSet(a);
InitSet(b);
InitSet(c);
InitSet(d);
ElemType r[8] = {12,32,13,1,2,4,7,8};
ElemType r1[8] = {25,98,26,15,46,38,5,1};
ElemType r2[8] = {5,98,25,8,34,1,15,46};
int i;
for(i=0;i<8;i++)
InsertSet(a,r[i]);
for(i=0;i<8;i++)
InsertSet(b,r1[i]);
for(i=0;i<8;i++)
InsertSet(c,r2[i]);
ElemType x=1,y=25;
DeleteSet(a,x);
DeleteSet(a,y);
cout<<"输出a集合的元素:"<<endl;
OutputSet(a);
cout<<"---------------------------------------------------------"<<endl;
if(ModifySet(a,13,30))
cout<<"把 a 集合的 13 更新为 30 更新成功 !"<<endl;
else
cout<<"集合 a 更新不成功 !"<<endl;
if(EmptySet(a))
cout<<"集合 a 为空 !"<<endl;
else
cout<<"集合 a 不为空 !"<<endl;
cout<<"集合a的长度:"<<LenthSet(a)<<endl;
cout<<"---------------------------------------------------------"<<endl;
cout<<"输出b集合的元素:"<<endl;
OutputSet(b);
cout<<endl;
cout<<"输出c集合的元素:"<<endl;
OutputSet(c);
cout<<endl;
cout<<"b、c两个集合的并集是:"<<endl;
UnionSet(b,c,d);
OutputSet(d);
cout<<endl;
cout<<"b、c两个集合的交集是:"<<endl;
InterseSet(b,c,d);
OutputSet(d);
cout<<endl;
cout<<"b、c两个集合的差集(c-b)是:"<<endl;
DifferenceSet(b,c,d);
OutputSet(d);
ClearSet(a);
ClearSet(b);
ClearSet(c);
ClearSet(d);
return 0;
}
❼ C语言怎么用函数求集合的交集
首先,如果是数学上的集合概念,那就说明,集合A自身的每个元素都不相同。
那么,程序就可以简化成,
设数组key[52],用于记录字母出现次数。
扫描一次集合A,把出现的字母计到key的对应位置里。
同理扫描一次集合B。
查看key数组,>=2的对应字母输出到集合C,C就是所求交集。
❽ 急~求集合之间的关系
指出下面各对集合之间的关系
A={x|x是等边三角形},B={x|x是等腰三角形}
A包含于B
用适当的符号(包含,不包含,等于,真子集)填空
1. {a}(包含于(即是真子集) ){a,b,c} 2.空集(包含于(即是真子集) ){1,2,3}
3. {x|x是矩形}( 包含){x|x是正方形}
4.{0,1}( 包含于(即是真子集))N 5.空集(包含于(即是真子集) ){0}
6.3(不包含 )空集
写出集合A={s,t}的所有子集
是
{s},{t},{s,t},空集.
❾ 用c语言判断两个整数集间的集合关系
/* Note:Your choice is C IDE */
#include "stdio.h"
main()
{
int i,j,m,n,count=0,temp=0,*a,*b,x;
printf("分别输入2个集合的大小");
scanf("%d%d",&m,&n);
if(m<n){temp=n,n=m,m=temp;} //为了判断方便,将大的数变成a数组的长度,小的为b数组的长度
a=(int *)malloc(sizeof(int)*m);
b=(int *)malloc(sizeof(int)*n);
for (i=0; i!=m; ++i)
{scanf("%d",&x);
a[i]=x; //输入整数集合的时候不能有相同的数字,这个你要的话可以自己加个判断
}
for (i=0; i!=n; ++i)
{scanf("%d",&x);
b[i]=x;
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if(a[i]==b[j])count++;
}
if(count==n){if(m>n){
printf("b是a的真子集");}
else
printf("b是a的子集");
if(count==n&&m==n)printf(",并且a等于b");
}
else
printf("a和b相互不包含");
}