㈠ 怎樣用語言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]='