C语言使用技巧(十七):将未排序与排序后的两个数组中的元素再取并集和交集

两个排序数组的并集和交集,实现源码:

// C program to find union of 
// two sorted arrays 
#include <stdio.h> 
  
/* Function prints union of arr1[] and arr2[] 
   m is the number of elements in arr1[] 
   n is the number of elements in arr2[] */
int printUnion(int arr1[], int arr2[], int m, int n) 
{ 
    int i = 0, j = 0; 
    while (i < m && j < n) { 
        if (arr1[i] < arr2[j]) 
            printf(" %d ", arr1[i++]); 
        else if (arr2[j] < arr1[i]) 
            printf(" %d ", arr2[j++]); 
        else { 
            printf(" %d ", arr2[j++]); 
            i++; 
        } 
    } 
  
    /* Print remaining elements of the larger array */
    while (i < m) 
        printf(" %d ", arr1[i++]); 
    while (j < n) 
        printf(" %d ", arr2[j++]); 
} 
  
/* Driver program to test above function */
int main() 
{ 
    int arr1[] = { 1, 2, 4, 5, 6 }; 
    int arr2[] = { 2, 3, 5, 7 }; 
    int m = sizeof(arr1) / sizeof(arr1[0]); 
    int n = sizeof(arr2) / sizeof(arr2[0]); 
    printUnion(arr1, arr2, m, n); 
    getchar(); 
    return 0; 
} 

在这里插入图片描述
没有排序的并集交集源码:相同的元素依次去重放在后面。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUMBER1 7
#define NUMBER2 5
void createCollect(int[], int);
void display(int[], int);
int main()
{
    int collection1[NUMBER1];
    int collection2[NUMBER2];
    int collection3[NUMBER1 + NUMBER2];
    int i, j, label = 0;
    createCollect(collection1, NUMBER1);

    for (j = 0; j < NUMBER2; j++)
    {
        collection2[j] = j * 2;
    }

    printf("集合1:");
    display(collection1, NUMBER1);

    printf("集合2:");
    display(collection2, NUMBER2);

    for (i = 0; i < NUMBER1; i++)
    {
        //从集合1中取出元素,去遍历集合2中的所有元素
        for (j = 0; j < NUMBER2; j++)
        {
            //如果相同则跳出遍历
            if (collection1[i] == collection2[j])
                break;
        }
        //判断:此时存在两种情况1) 当前集合1的元素与集合2的元素相同
        //2)遍历完了集合2的数组后不存在相同的元素
        if (j == NUMBER2)
        {
            collection3[label] = collection1[i];
            label++;
        }
    }

    //把集合2的所有元素存进collection3中
    for (j = 0; j < NUMBER2; j++)
    {
        collection3[label++] = collection2[j];
    }

    printf("集合1与集合2的并集为:\n");
    display(collection3, label);
    getchar();
    return 0;
}
//随机生成一个不含重复元素的数组
void createCollect(int num[], int count)
{
    //randValue:临时随机数存放变量
    //condition:循环生成不重复的条件
    int i, j, randValue, condition;
    srand(time(NULL));
    for (i = 0; i < count; i++)
    {
        condition = 1;
        while (condition)
        {
            randValue = 1 + (int)rand() % 10;
            for (j = 0; j <= i; j++)
            {
                if (i == 0)
                { //第一个数不可能存在重复数,可以直接赋值
                    condition = 0;
                    break;
                }
                if (randValue == num[j])
                    break;
                if (randValue != num[j] && j == i - 1) //当生成的随机数与当前数组最后一位不同并且下标相等时
                {
                    condition = 0;
                    break;
                }
            }
        }
        num[i] = randValue;
    }
}
//打印数组
void display(int num[], int count)
{
    int i;
    for (i = 0; i < count; i++)
    {
        printf("%d\t", num[i]);
    }
    printf("\n");
}

执行结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源代码杀手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值