當前位置:首頁 » 編程語言 » c語言集合的差

c語言集合的差

發布時間: 2023-01-31 10:15:56

c語言 集合的差,交,並,笛卡爾積的運算。例如A={2,3,4,5,6,7,8}B={2,3,4,5,11,25}

交:C={2,3,4,5} 就是既屬於A的又屬於B的那部分
並:C = {2,3,4,5,6,7,8,11,25} 兩個集合的整合去掉重復的。A+B-AB(AB:公共部分)
差:C= {6,7,8}就是屬於A但是不屬於B的那部分
笛卡爾乘積:這個得出的集合就多了:舉個例子。。假設集合A={a,b},集合B={c,d}則兩個集合的笛卡爾積為{(a,c),(a,d),(b,c),(b,d)}

Ⅱ 用C語言編寫一個集合的交,並和差運算的程序怎麼寫啊

/*第一,你的題意不明,我只能輸入兩個集合了【互異性由輸入保證】*/
#include<stdio.h>
#include<string.h>
void main()
{
char temp[60]="",str1[30]="",str2[30]="",i,j,l1,l2,ch;
printf("STR1:");
gets(str1);
printf("STR2:");
gets(str2);
l1=strlen(str1);
l2=strlen(str2);
//交集
printf(" 交集: {");
for(i=0;i<l1;i++)
for(j=0;j<l2;j++)
if(str1[i]==str2[j]) printf("%c,",str1[i]);
printf("} ");

//並集 偷懶的演算法: 合並->排序->刪除相同
printf(" 並集: {");

/*合並*/sprintf(temp,"%s%s",str1,str2);
/*排序*/
for(i=0;i<l1+l2-1;i++)
for(j=i+1;j<l1+l2;j++)
if(temp[i]>temp[j])
{
char ch;
ch=temp[i];
temp[i]=temp[j];
temp[j]=ch;
}
/*刪除相同字元*/
for(i=j=1;i<l1+l2;i++)
if(temp[i]!=temp[j-1]) temp[j++]=temp[i];
temp[j]=''
for(i=0;i<j;i++)
printf("%c,",temp[i]);
printf("} ");
//CuA
printf(" CuA: {");
for(ch='a'ch<='z'ch++)
{
for(i=0;i<l1;i++)
if(ch==str1[i]) goto NOT;
printf("%c,",ch);
NOT:if(0);
}
printf("} ");
//CuB
printf(" CuB: {");
for(ch='a'ch<='z'ch++)
{
for(i=0;i<l2;i++)
if(ch==str2[i]) goto NOT2;
printf("%c,",ch);
NOT2:if(0);
}
printf("} ");
}

Ⅲ 數據結構 用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"

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("} ");
}

熱點內容
安卓下載管理器哪個好 發布:2024-04-25 23:22:48 瀏覽:441
考試系統源碼php 發布:2024-04-25 23:09:46 瀏覽:135
磁碟禁止訪問 發布:2024-04-25 22:53:48 瀏覽:286
多線程ftp上傳 發布:2024-04-25 22:41:36 瀏覽:114
phpqrcode 發布:2024-04-25 22:41:36 瀏覽:32
桂平上網密碼是多少 發布:2024-04-25 22:32:10 瀏覽:574
open函數c語言 發布:2024-04-25 21:47:42 瀏覽:406
簡訊刪除後怎麼找伺服器 發布:2024-04-25 21:15:06 瀏覽:388
查ip地址伺服器數量 發布:2024-04-25 20:49:48 瀏覽:620
安卓手機單核性能為什麼不高 發布:2024-04-25 20:48:07 瀏覽:56