當前位置:首頁 » 編程語言 » 集合的交並差c語言心得
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

集合的交並差c語言心得

發布時間: 2022-05-28 09:36:52

㈠ 請問如何用c語言代碼實現兩個集合的交,並,相對補,對稱差的運算並判斷兩個集合是否相等

先取到兩個集合的數組或是指針,循環遍歷各個元素,至於二者的交、並、補可以用與、或、非來實現,實現方式大概就是這樣。

㈡ 數據結構 用c語言寫的 集合的並、交和差運算的程序

可以用二個一維數組,
再用兩個for循環來判斷結果:交,並,差
在for循環中,用一個if來判斷一下,是不是a[0]==b[j],只要有相等的,就令之放在c[0]
這就是交集!!

並集就好求吧,
只要令c[i]=a[i],再來一個就是c[i+j+1]=b[j](因為我這里是考慮j=0開始的,然後自加差就是在交上改動一下就可以了,只要是a[0]!=b[j],就把它放到c[]這個數組裡面去~!!!!

1:並集的程序。

求集合LA和集合LB的並集

#define NULL 0

struct JD
{ int data;
struct JD *next;
};

int find(int number,struct JD *h)
{ while(h->data)
{ if(h->data!=number)
{ h=h->next;
continue;
}
else
return 0;
}
return 1;
}

struct JD * make()
{ struct JD *h=NULL,*p=NULL;
int number,tf;
h=(struct JD *)malloc(sizeof(struct JD));
scanf("%d",&h->data);
p=h;
while(p->data)
{ p->next=(struct JD *)malloc(sizeof(struct JD));
p=p->next;
p->data=0;
scanf("%d",&number);
tf=find(number,h);
if(tf)
p->data=number;
else
continue;
}
return h;
}

void print(struct JD *h)
{ while(h->data)
{ printf("%d ",h->data);
h=h->next;
}
}

struct JD * change(struct JD *la,struct JD *lb)
{ struct JD *h,*p,*s,*q;
int number,tf;
p=lb;
while(p->data)
{ number=p->data;
tf=find(number,la);
p=p->next;
if(tf)
{ s=(struct JD *)malloc(sizeof(struct JD));
s->data=number;
s->next=la;
la=s;
}
else
continue;
}
return la;
}

void del(struct JD *h)
{ struct JD *p=h->next;
while(h->data)
{ free(h);
h=p;
p=p->next;
}
free(h);
}

main()
{ struct JD *la,*lb;
printf("\n\nGive the number to LA :\n\n");
la=make();
printf("\nLA is: ");
print(la);
printf("\n\nGive the number to LB :\n\n");
lb=make();
printf("\nLB is: ");
print(lb);
la=change(la,lb);
printf("\n\n\nThe new LA=LA||LB is: ");
print(la);
del(la);
del(lb);
printf("\n\n\nPass any key to exit...!\n");
getch();
}

********** 程序運行結果 **********
Give the number to LA :
1↓
2↓
3↓
5↓
0↓

LA is: 1 2 3 5

Give the number to LB :

6↓
7↓
3↓
2↓
9↓
0↓

LB is: 6 7 3 2 9

The new LA=LA||LB is: 9 7 6 1 2 3 5

--------------------------------------------------
Pass any key to exit...!

㈢ 單鏈表表示的集合交,並,差運算,設計採用定義集合,用集合運算表達式求值的方式進行。C語言實現。

#include<stdio.h>
#include<stdlib.h>
typedef
struct
LNode//
定義結構體類型指針
{
char
data;
struct
LNode*next;
}*pointer;
void
readdata(pointer
head)//
定義輸入集合函數
{
pointer
p;
char
tmp;
scanf("%c",&tmp);
while(tmp!='\n')
{
p=(pointer)malloc(sizeof(struct
LNode));
p->data=tmp;
p->next=head->next;
head->next=p;
scanf("%c",&tmp);
}
}
void
pop(pointer head)//定義輸出集合函數
{
pointer
p;
p=head->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
void
or(pointer head1,pointer head2,pointer head3)//定義集合的交集函數
{
pointer
p1,p2,p3;
p1=head1->next;
while(p1!=NULL)
{
p2=head2->next;
while((p2!=NULL)&&(p2->data!=p1->data))
p2=p2->next;
if((p2!=NULL)&&(p2->data==p1->data))
{
p3=(pointer)malloc(sizeof(structLNode));
p3->data=p1->data;
p3->next=head3->next;
head3->next=p3;
}
p1=p1->next;
}
}
void main()//主函數
{
int x;
printf("(輸入數據,按回車鍵結束\n");
pointerhead1,head2,head3;
head1=(pointer)malloc(sizeof(structLNode));
head1->next=NULL;
head2=(pointer)malloc(sizeof(structLNode));
head2->next=NULL;
head3=(pointer)malloc(sizeof(structLNode));
head3->next=NULL;printf("請輸入集合A:\n");
readdata(head1);//調用輸入集合函數
printf("請輸入集合B:\n");
readdata(head2);//調用輸入集合函數A:
printf("1.交集2.結束\n");
do
{
printf("請選擇序號\n");
scanf("%d",&x);
switch(x)
{
case1:
or(head1,head2,head3);//調用交集函數
printf("集合A為:");
pop(head1);
printf("集合B為:");
pop(head2);
printf("兩集合的交集C=A∩B:");
pop(head3);
head3->next=NULL;
break;
case2:
break;
default:
gotoA;
}
}while(x!=2);
}

㈣ 實驗、集合的交、並差 用c語言

#include"stdio.h"

intinput(intd[],intn)
{
inti;
//n=0;
do
{
scanf("%d",d+n);
n+=1;
}while(d[n-1]>=0);
d[n-1]='';
returnn-1;
}
voidsort(intd[],int*n)
{
inti,j,t,k;
for(i=0;i<*n-1;i++)
{
for(j=*n-1;j>i;j--)
{ if(d[j]==d[j-1])
{
*n-=1;
for(k=j;k<*n;k++)
d[k]=d[k+1];
}
if(d[j]<d[j-1])
{
t=d[j];d[j]=d[j-1];d[j-1]=t;
}

}
}
}
intfn(intd1[],intnum1,intd2[],intnum2)
{
inti,j,m;

for(i=0;i<num1;i++)
{
m=0;
for(j=0;j<num2;j++)
{
if(d1[i]==d2[j])
{
m=1;
break;
}
}
if(m==0)
printf("%d,",d1[i]);
}
}
intmain()
{
intA[100],B[100],C[200];
intnuma,numb,n;
inti,j;
//輸入
printf("inputsortA:");
numa=input(A,0);
sort(A,&numa);
printf("inputsortB:");
numb=input(B,0);
sort(B,&numb);
//交集
printf("集合交集A∩B={");
for(i=0;i<numa;i++)
{
for(j=0;j<numb;j++)
{
if(A[i]==B[j])
{
printf("%d,",A[i]);
}

}
}
printf("} ");
//並集
n=numa+numb;
printf("集合並集A∪B={");
for(i=0;i<numa;i++)
{
C[i]=A[i];
}
for(i=numa;i<n;i++)
{
C[i]=B[i-numa];
}
sort(C,&n);
for(i=0;i<n;i++)
printf("%d,",C[i]);
printf("} ");
//集合差
printf("A-B={");
fn(A,numa,B,numb);
printf("} ");
}

㈤ 求任意兩個正數集合的並、交和差集。用C語言做。

數字有什麼特徵:
1. 都是正整數么?
2. 數字的范圍是多少?
3. 每個結合內部有沒有重復的數字?

如果都是正整數,並且每個集合都沒有重復數字,那問題就相當好辦了,用直接存儲的數據結構即可。

「交:從一個集合中取出一個元素,在另一個集合中查找,如果有它就是交中的並:並也類似,關鍵就是判斷這個元素是否都在這兩個集合中出現。差:差更 」

這種方式效率太低,當結合非常大的時候,時間復雜度回事O(N^2),用直接存儲可以保證時間復雜度為O(N)。

㈥ 集合的並,交,差以及它們的混合運算的演算法(c語言)

/*diff.c:差運算*/
#include<stdio.h>
#include<stdlib.h>
intset_a[5]={1,3,5,9,7};//非0元素構成的集合
intset_b[5]={1,5,2,8,4};
intset_c[10]={0};
intisin(inta_or_b,intelem)
{
int*set_tmp;
intcnt;
switch(a_or_b)
{
case1:
set_tmp=set_b;
break;
case2:
set_tmp=set_a;
break;
default:
printf("parametererror! ");
}
for(cnt=0;cnt<5;cnt++)
{
if(set_tmp[cnt]==elem)
{
return1;
}
}
return0;
}
intmain(intargc,char*argv[])
{
intcnt,cnt2=0;
for(cnt=0;cnt<5;cnt++)
{
if(isin(1,set_a[cnt])==1)//如果集合a中的元素在集合b中
{
set_c[cnt2++]=set_a[cnt];
}
}
printf("Theresultofdiff: ");
for(cnt=0;set_c[cnt]!=0;cnt++)//輸出非0元素
{
printf("%d ",set_c[cnt]);
}
putchar(' ');
return0;
}

親是要這樣做差運算嗎?

㈦ 怎樣用語言c語言實現集合的合並,交集

通過你描述的問題,正確的交集代碼如下:
void
bing(char
a[],char
b[],int
m,int
n)
{
char
d[400];
int
i=0,j=0,s=m;
for(i=0;i<m;i++)
d[i]=a[i];
for(i=0;i<n;i++){
for(j=0;j<m;j++)
{
if(b[i]==a[j])
break;
}
if(j==m)
d[s++]=b[i];
}
cout<<"集合並集是:";
for(i=0;i<s;i++)
cout<<d[i]<<"
";
}

㈧ C語言編寫集合的並交差

int a1[5]={1, 2, 4, 7, 8};
int a2[6]={3, 2, 5, 8, 9, 0};
int a3[11];
//並 a3[11] = [1, 2, 3, 4, 5, 7, 8, 9, 0] ,返回值為長度:9
int Bing(int *p1, int nlen1, int *p2,int nlen2, int *pdest)
{
int nPos;
bool bFind;

if((!p1) || (!p2) || (!pdest))
return 0;
for(nPos=0; nPos<nlen1; nPos++)
{
pdest[nPos] = p1[nPos];
}

for(int i=0; i<nlen2; i++)
{
bFind = false;
for(int j=0; j<nPos; j++)
{
if(pdest[j] == p2[i])
{
bFind = true;
break;
}
}
if(!bFind)
pdest[nPos++] = p2[i];
}
return nPos;
}

//交 a3[11] = [2, 8]
int Jiao(int *p1, int nlen1, int *p2, int nlen2, int *pdest)
{
int nPos;
bool bFind;

if((!p1) || (!p2) || (!pdest))
return 0;
nPos = 0;
for(int i=0; i<nlen1; i++)
{
for(int j=0; j<nlen2; j++)
{
if(p1[i] == p2[j])
pdest[nPos++] = p1[i];
}
}
return nPos;
}

//int a1[5]={1, 2, 4, 7, 8};
//int a2[6]={3, 2, 5, 8, 9, 0};
//差. a3[11] = [1, 4, 7]
int Cha(int *p1, int nlen1, int *p2, int nlen2, int *pdest)
{
int nPos;
bool bFind;

if((!p1) || (!p2) || (!pdest))
return 0;
nPos = 0;
bFind = false;
for(int i=0; i<nlen1; i++)
{
bFind = false;
for(int j=0; j<nlen2; j++)
{
if(p1[i] == p2[j])
bFind = true;
}
if(!bFind)
pdest[nPos++] = p1[i];
}
return nPos;
}

void PrintArray(int *p, int nlen)
{
if(!p)
return;
for(int i=0; i<nlen; i++)
printf("%d ", p[i]);
printf("\n");
}
void main()
{
int nlen;

nlen = Bing(a1, 5, a2, 6, a3);
PrintArray(a3, nlen);

nlen = Jiao(a1, 5, a2, 6, a3);
PrintArray(a3, nlen);

nlen = Cha(a1, 5, a2, 6, a3);
PrintArray(a3, nlen);
}

㈨ C++在類中實現集合的交,並,差運算,怎麼做呀

1,是否屬於集合,就拿這個元素和集合裡面的比較啊,如果想等就存在。
2,實現並,就是拿一個集合A裡面的一個元素,和另外一個集合B的元素比較,如果想等就不要這個A中元素。不想等就把A中這個元素加入到B中,前提是B數組足夠大。也可以再建立個C集合。
3,實現交 ,就是把集合A中元素和集合B中元素想等的取出來,放到C中。
上面主要是利用for 循環進行。