❶ 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相互不包含");
}