zoukankan      html  css  js  c++  java
  • 山科日记—编写函数unionSet(编程题)

    Description

    编写一个函数unionSet,对2个集合求并集。其原型为:

    int unionSet(int setA[],int setB[],int numA,int numB);

    其中setAsetB是两个待合并的集合,numAnumB分别是2个集合的元素个数。该函数将两个集合合并后,放在数组setA中,返回值为并集中的元素个数。

    Input

    输入为2行,每行是一个集合,每行的输入以数值0作为结束,个数不超过100个。2个集合合并后的元素取值均在[1,100]内。

    Output

    输出2个集合求并后的结果,两两之间用一个空格隔开。输出时,先输出第一个集合中的元素,再输出从第2个集合中合并过来的元素。见样例。

    Sample Input

    1 2 3 4 5 7 0

    2 4 6 8 19 0

    Sample Output

    1 2 3 4 5 7 6 8 19

    HINT

    Append Code

    append.c,

    int main()

    {

        int setA[101],setB[101],numA,numB,i;

        numA=numB=0;

        scanf("%d",&setA[numA]);

        while (setA[numA]!=0)

        {

            numA++;

            scanf("%d",&setA[numA]);

        }

        scanf("%d",&setB[numB]);

        while (setB[numB]!=0)

        {

            numB++;

            scanf("%d",&setB[numB]);

        }

        numA=unionSet(setA,setB,numA,numB);

        printf("%d",setA[0]);

        for (i=1; i<numA; i++)

            printf(" %d",setA[i]);

        return 0;

    }

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    int unionSet(int setA[],int setB[],int numA,int numB)
    {
        int num[1000];
        int i, t=0, j;
        for(i=0;i<numB;i++)
        {
            t=0;
            for(j=0;j<numA;j++)
            {
                if(setA[j]==setB[i])
                    t++;
            }
            if(t==0)
            {
                 setA[numA++]=setB[i];
            }
    
    
        }
    
        return numA;
    
    
    }
    int main()
    {
        int setA[101],setB[101],numA,numB,i;
        numA=numB=0;
        scanf("%d",&setA[numA]);
        while (setA[numA]!=0)
        {
            numA++;
            scanf("%d",&setA[numA]);
        }
        scanf("%d",&setB[numB]);
        while (setB[numB]!=0)
        {
            numB++;
            scanf("%d",&setB[numB]);
        }
        numA=unionSet(setA,setB,numA,numB);
        printf("%d",setA[0]);
        for (i=1; i<numA; i++)
            printf(" %d",setA[i]);
        return 0;
    }
    

      

    作者:7oDo

    仅供参考,请勿抄袭。

    Hang Hang Hang !!!

  • 相关阅读:
    python使用urllib2抓取防爬取链接
    Python 标准库 urllib2 的使用细节
    源代码阅读利器:Source Navigator — LinuxTOY
    python程序使用setup打包安装 | the5fire的技术博客
    mctop: 监视 Memcache 流量 — LinuxTOY
    nload: 监视网络流量及带宽占用
    Learn Linux The Hard Way — LinuxTOY
    使用virtualenv创建虚拟python环境 | the5fire的技术博客
    autocompleteredis 基于redis的自动补全 开源中国
    atool home
  • 原文地址:https://www.cnblogs.com/Jie-Fei/p/8298876.html
Copyright © 2011-2022 走看看