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]='