2个回答

# 如果问题得到解决，请点我回答左上角的采纳和向上的箭头，谢谢

``````#include "stdio.h"
#define MAX 100

void Union(int *r, int *rn, int *a1, int n1, int *a2, int n2)
{
if (n1 == 0 && n2 == 0)
{
*rn = 0;
return;
}
*rn = 1;
r[0] = (n1 != 0) ? a1[0] : a2[0];
for (int i = 0; i < n1; i++)
{
int ex = 0;
for (int j = 0; j < *rn; j++)
{
if (r[j] == a1[i])
{
ex = 1;
break;
}
}
if (!ex)
{
r[*rn] = a1[i];
(*rn)++;
}
}
for (int i = 0; i < n2; i++)
{
int ex = 0;
for (int j = 0; j < *rn; j++)
{
if (r[j] == a2[i])
{
ex = 1;
break;
}
}
if (!ex)
{
r[*rn] = a2[i];
(*rn)++;
}
}

}

int main()
{
int a[] = {1,2,3,4};
int b[] = {3,4,5,6,7,7};
int result[MAX];
int n;
Union(result, &n, a, 4, b, 6);
for (int i = 0; i < n; i++)
printf("%d ", result[i]);
return 0;
}
``````

1 2 3 4 5 6 7 Press any key to continue . . .

#include
#define N 100

int main()
{
int a[N],b[N],c[N],d[N],e[N],f[N];
int m,n,i,j;
int k=1,x=1,y=1;
int flag;

``````printf("请确定集合A中的个数:\n");
scanf("%d",&m);
printf("请输入集合A中的数:\n");
for(i=1;i<=m;i++)
{
scanf("%d",&a[i]);
}

printf("请确定集合B中的个数:\n");
scanf("%d",&n);
printf("请输入集合B中的数:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}

for(i=1;i<=m;i++)
{
flag=0;
for(j=1;j<=n;j++)
{
if(a[i]==b[j])
{c[k]=a[i];k++;flag=1;continue;}
}
if(flag==0)
{
e[x]=a[i];x++;
}
}

for(i=1;i<=n;i++)
{
flag=0;
for(j=1;j<=m;j++)
{
if(a[j]==b[i])
{flag=1;continue;}
}
if(flag==0)
{
f[y]=b[i];y++;
}
}

printf("A与B的交集:共%d个\n",k-1);
for(i=1;i<k;i++)
{printf("%d ",c[i]);}
printf("\n");

printf("A与B的并集:共%d个\n",x+y+k-3);
for(i=1;i<x;i++)
{printf("%d ",e[i]);}
for(i=1;i<y;i++)
{printf("%d ",f[i]);}
for(i=1;i<k;i++)
{printf("%d ",c[i]);}
printf("\n");
``````