编写程序,求两个整数集合的并集。。。

编写程序,求两个整数集合的并集。。。能不能把下面这个修改一下??如果可以,再写一个完整的程序也可以图片图片

这是按照你的思路写的(假设a b两个数组内没有重复的数字)

#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;
} 

这种问题不要贴图,请给出程序,总不至于让人帮你再敲一遍吧。

我写了一个另外的,先连接两个数组,排序,然后去掉重复的。

#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