㈠ 怎样用语言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语言数组实现。要求效率高点的。。最好有注释。。谢谢啦。。超高分。。
#include<stdio.h>
#include<malloc.h>
typedefstructnode{
intnum;
structnode*next;
}AGG;
AGG*CreateList(){//创建单循环链表,返回链表头
AGG*head,*p;
inti,n;
printf("结点个数n=");
scanf("%d",&n);
head=p=(AGG*)malloc(sizeof(AGG));//专用头结点
head->num=0;
printf("输入%d整数(空格隔开): ",n);
for(i=0;i<n;++i){
p->next=(AGG*)malloc(sizeof(AGG));
scanf("%d",&p->next->num);
p=p->next;
}
p->next=head;
returnhead;
}
voidRiseSort(AGG*head){//上升排序
AGG*p,*s,*pt;
p=head;
s=p->next;
while(p->next!=head){
while(s->next!=head){
if(p->next->num>s->next->num){
pt=p->next;
p->next=s->next;
s->next=p->next->next;
p->next->next=pt;
}
elses=s->next;
}
p=p->next;
s=p->next;
}
}
voidSimplification(AGG*head){//去除相同的集合元素
AGG*p,*q,*s;
p=head->next;
q=p->next;
while(q!=head){
if(p->num==q->num){
p->next=q->next;
s=q;
q=q->next;
deletes;
}
else{
p=p->next;
q=q->next;
}
}
}
AGG*CreateAgg(){
AGG*head;
head=CreateList();
RiseSort(head);;
Simplification(head);
returnhead;
}
voidInsertNode(AGG*head,intnum){
AGG*t,*p=head;
while(p->next!=head){
if(p->next->num==num)return;
if(p->next->num<num)p=p->next;
else{
t=(AGG*)malloc(sizeof(AGG));
t->num=num;
t->next=p->next;
p->next=t;
return;
}
}
t=(AGG*)malloc(sizeof(AGG));
t->num=num;
p->next=t;
t->next=head;//插入在链表尾的处理
}
AGG*MergeAgg(AGG*A,AGG*B){//A∪B
AGG*head,*pa,*pb,*pc,*qc;
head=pc=(AGG*)malloc(sizeof(AGG));
pa=A->next;
while(pa!=A){
qc=(AGG*)malloc(sizeof(AGG));
qc->num=pa->num;
pc->next=qc;
pc=qc;
pa=pa->next;
}
pc->next=head;
pb=B->next;
while(pb!=B){
InsertNode(head,pb->num);
pb=pb->next;
}
returnhead;
}
AGG*MutualAgg(AGG*A,AGG*B){//A∩B
AGG*C,*pa,*pb,*pc,*qc;
C=pc=(AGG*)malloc(sizeof(AGG));
pc->num=0;
pa=A->next;
pb=B;
while(pa!=A){
pb=B->next;
while(pb!=B){
if(pb->num==pa->num){
qc=(AGG*)malloc(sizeof(AGG));
qc->num=pb->num;
pc->next=qc;
pc=qc;
}
pb=pb->next;
}
pa=pa->next;
}
pc->next=C;
returnC;
}
AGG*DifferAgg(AGG*A,AGG*B){//返回A、B的差集A-B
AGG*head,*p,*q,*r;
inttag;
head=r=(AGG*)malloc(sizeof(AGG));
for(p=A->next;p!=A;p=p->next){
tag=1;
for(q=B->next;q!=B&&tag;q=q->next)
tag=p->num!=q->num;
if(tag){
r->next=(AGG*)malloc(sizeof(AGG));
r=r->next;
r->num=p->num;
}
}
for(p=B->next;p!=B;p=p->next){
tag=1;
for(q=A->next;q!=A&&tag;q=q->next)
tag=p->num!=q->num;
if(tag){
r->next=(AGG*)malloc(sizeof(AGG));
r=r->next;
r->num=p->num;
}
}
r->next=head;
RiseSort(head);
returnhead;
}
voidPrintList(AGG*head){
AGG*p=head->next;
shortcounter=0;
while(p!=head){
if(counter&&counter%10==0)printf(" ");
printf("%d",p->num);
counter++;
p=p->next;
}
if(counter%10)printf(" ");
}
voidfreeheap(AGG*head){
AGG*p,*q;
p=head;
q=p->next;
while(q!=head){
p=q;
q=p->next;
free(p);
}
free(head);
}
intmain(){
AGG*A,*B,*C,*D,*E;
printf("创建集合A: ");
A=CreateAgg();
printf("创建集合B: ");
B=CreateAgg();
printf("集合A的元素有: ");
PrintList(A);
printf("集合B的元素有: ");
PrintList(B);
C=MutualAgg(A,B);
printf("交集C=A∩B: ");
PrintList(C);
printf("并集D=A∪B: ");
D=MergeAgg(A,B);
PrintList(D);
printf("差集D=A-B: ");
E=DifferAgg(A,B);
PrintList(E);
freeheap(A);
freeheap(B);
freeheap(C);
freeheap(D);
freeheap(E);
printf(" ");
return0;
}
㈢ 关于集合的交集并集差集的C语言程序
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
using namespace std;
int main()
{
int a[]={1,5,8,12,5,-5,32};
int b[]={3,5,1,-3,10};
list< int > set1(a,a+sizeof(a)/sizeof(int));
list< int > set2(b,b+sizeof(b)/sizeof(int));
list< int > result;
set1.sort();
set2.sort();
//交集
set_intersection(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
(result.begin(),result.end(),ostream_iterator< int >(cout," "));
cout<<endl;
result.clear();
//并集
set_union(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
(result.begin(),result.end(),ostream_iterator< int >(cout," "));
cout<<endl;
result.clear();
//差集
set_difference(set1.begin(),set1.end(),set2.begin(),set2.end(),back_inserter(result));
(result.begin(),result.end(),ostream_iterator< int >(cout," "));
return 0;
}
㈣ 实验、集合的交、并差 用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]='