ji1812062521 于 2016.02.19 02:04 提问

4个回答

caozhy      2016.02.19 04:01

``````#include <iostream>
#include <stdlib.h>
using namespace std;

void arrunion(int a[], int b[], int r[], int an, int bn, int * n)
{
*n = an;
memcpy(r, a, an * sizeof(int));
for (int i = 0; i < bn; i++)
{
bool f = true;
for (int j = 0; j < an; j++)
{
if (r[j] == b[i]) { f = false; break; }
}
if (f) { r[*n] = b[i]; *n = *n + 1; }
}
}

int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int b[10] = {3,4,5,6,7,8,10,11,8,3};
int c[20];
int n = 0;
arrunion(a, b, c, 10, 10, &n);
for (int i = 0; i < n; i++)
cout << c[i] << " ";
cout << endl;
return 0;
}
``````
caozhy      2016.02.19 03:03

ji1812062521
2 年多之前 回复
caozhy      2016.02.19 03:45

``````#include <iostream>
#include <stdlib.h>
using namespace std;

int cmp(const void * a, const void * b)
{
return (*(int *)a) - (*(int *)b);
}

void arrunion(int a[], int b[], int r[], int an, int bn, int * n)
{
int dif = 0;
memcpy(r, a, an * sizeof(int));
memcpy(r + an, b, bn * sizeof(int));
qsort(r, an + bn, sizeof(int), cmp);
for (int i = 1; i < (an + bn); i++)
{
if (r[i] == r[i - 1 - dif])
{
dif++;
}
else
{
r[i - dif] = r[i];
}
}
*n = an + bn - dif;
}

int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int b[10] = {3,4,5,6,7,8,10,11,8,3};
int c[20];
int n = 0;
arrunion(a, b, c, 10, 10, &n);
for (int i = 0; i < n; i++)
cout << c[i] << " ";
cout << endl;
return 0;
}
``````

1 2 3 4 5 6 7 8 9 10 11
Press any key to continue

dongfeng9ge      2016.02.23 15:21

java求两个集合的交集和并集

A、B两个整数集合的交集