本人碰见一道C语言难题,寻大神帮助,利用C语言实现:求任意两个集合的交集、并集、差集, C语言大神求解。。一道集合求并集

\u672c\u4eba\u78b0\u89c1\u4e00\u9053C\u8bed\u8a00\u96be\u9898\uff0c\u5bfb\u5927\u795e\u5e2e\u52a9\uff0c\u5229\u7528C\u8bed\u8a00\u5b9e\u73b0\uff1a\u6c42\u4efb\u610f\u4e24\u4e2a\u96c6\u5408\u7684\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6\uff0c\u5144\u5f1f\u611f\u6fc0\u4e0d\u5c3d

\u53ef\u4ee5\u5229\u7528\u6570\u7ec4\u5b58\u653e2\u4e2a\u96c6\u5408\u6bd4\u5982\u6570\u7ec4A\uff0c\u548c\u6570\u7ec4B\uff0c\u4ea4\u96c6\u5c31\u662f\u904d\u5386\u5bfb\u627e\u4e24\u4e2a\u76f8\u540c\u7684\u6570\u5b57\uff08\u4e24\u6b21\u5faa\u73af\uff09\uff0c\u4fdd\u5b58\u5230\u53e6\u4e00\u4e2a\u6570\u7ec4\u4e2d\u5c31\u53ef\u4ee5\u4e86\uff0c
\u5e76\u96c6\u5c31\u662f\u4eceA\u6570\u7ec4\u4e2d\u53d6\u51fa\u6bcf\u4e2a\u5143\u7d20\u53bb\u6570\u7ec4B\u6bd4\u8f83\uff0c\u5982\u679cA\u4e2d\u6ca1\u6709\u5219\u6dfb\u52a0\u8fdb\u53bb
\u540c\u7406\u4f60\u6839\u636e\u5dee\u96c6\u7684\u5b9a\u4e49\u5c31\u53ef\u4ee5\u89e3\u51b3

#includeint main (){int i,k,j,a[4],b[4],c[8];printf("\u8bf7\u8f93\u5165\u96c6\u5408a\u7684\u503c\uff1a");for(i=0;i= 4){//\u82e5\u6ca1\u6709\u51fa\u73b0\u8fc7\u5219\u52a0\u5165\u96c6\u5408 c[k] = b[j]; k++; } } /*--add end--*/ while(k!=0) { --k; //changed printf("%d ",c[k]); } return 0;}

以前写过一个纯C的, 用的是数组,模拟C++ STL里面的set_intersection,set_union和set_difference的实现。 稍作了修改,添加了些注释,希望能帮到你。注意:必须先对输入集合排序;输出结果和C++ STL的测试结果吻合。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int set_intersection (const int Set1[], const unsigned int SizeofSet1,
                      const int Set2[], const unsigned int SizeofSet2,
                      int Res[], unsigned int* pSizeofRes);
int set_union (const int Set1[], const unsigned int SizeofSet1,
                      const int Set2[], const unsigned int SizeofSet2,
                      int Res[], unsigned int* pSizeofRes);                     
int set_difference (const int Set1[], const unsigned int SizeofSet1,
                      const int Set2[], const unsigned int SizeofSet2,
                      int Res[], unsigned int* pSizeofRes);                     
int compare (const void * a, const void * b);
void print_array(const int arr[], const size_t len);
int main(int argc, char** argv)
{
    int first[] = {5,10,15,20,25};
    int second[] = {50,40,30,20,10};
    unsigned int size1, size2, size_intxn, size_union, size_diff, retcode;
    int *pr_intxn, *pr_union, *pr_diff;
    /* Pre-requirement, set MUST be sorted. */
    size1 = sizeof(first) / sizeof(first[0]);
    size2 = sizeof(second) / sizeof(second[0]);
    qsort(first, size1, sizeof(int), compare);
    qsort(second, size2, sizeof(int), compare);
           
    /* Intersection */
    size_intxn = (size1 > size2) ? size1 : size2; /* estimate size of result */
       
    pr_intxn = (int *)malloc(sizeof(int) * size_intxn); /* pre-allocate result */
    if (NULL == pr_intxn) {
        printf("Intersection memory error.
");
        return -1;
    }
           
    printf("1) Set of Intersection:
");   
    retcode = set_intersection(first, size1, second, size2, pr_intxn, &size_intxn);
    if (retcode == 0)
        print_array(pr_intxn, size_intxn);
    else
       printf("Error in set_intersection, code %d
", retcode);
          
    free(pr_intxn);
       
    /* Union */
    size_union = size1 + size2; /* estimate size of result */
       
    pr_union = (int *)malloc(sizeof(int) * size_union); /* pre-allocate result */
    if (NULL == pr_union) {
        printf("Union memory error.
");
        return -1;
    }
           
    printf("2) Set of Union:
");   
    retcode = set_union(first, size1, second, size2, pr_union, &size_union);
    if (retcode == 0)
       print_array(pr_union, size_union);
    else
       printf("Error in set_union, code %d
", retcode);
    free(pr_union);
       
    /* Difference */
    size_diff = size1 + size2; /* estimate size of result */
       
    pr_diff = (int *)malloc(sizeof(int) * size_diff); /* pre-allocate result */
    if (NULL == pr_diff) {
        printf("Difference memory error.
");
        return -1;
    }
           
    printf("3) Set of Difference:
");   
    retcode = set_difference(first, size1, second, size2, pr_diff, &size_diff);
    if (retcode == 0)
       print_array(pr_diff, size_diff);
    else
       printf("Error in set_difference, code %d
", retcode);
           
    free(pr_diff);
       
       
    return 0;
}
/*
  Input:
    Set1 - First set.
    Set2 - Second set.
    SizeofSet1 - Set length of First set.
    SizeofSet2 - Set length of Second set.
  Input/Output: 
    Res - Set for storing results.
    pSizeofRes - Point to SizeofRes, length of result. If SizeofRes is less than
                 expected, a proper size will be returned to caller.
  Return:
    0 - If successfully.              
    1 - SizeofRes is smaller than expected.
    -1 - Internal memory error.
*/
int set_difference (const int Set1[], const unsigned int SizeofSet1,
                      const int Set2[], const unsigned int SizeofSet2,
                      int Res[], unsigned int* pSizeofRes)
{
    int i, j, k;
    unsigned int size_pre;   
    int *pr = 0;
       
    size_pre = SizeofSet1 + SizeofSet2;
    if ( *pSizeofRes < size_pre)
    {
        *pSizeofRes = size_pre;
        return 1;
    }
       
    pr = (int *)malloc(size_pre * sizeof(int));
    if ( pr == NULL )
       return -1;
          
    i = 0; j = 0; k = 0;
    while ( i < SizeofSet1 && j < SizeofSet2 )
    {
        if (Set1[i] < Set2[j]) pr[k++] = Set1[i++];       
        else if (Set2[j] < Set1[i]) ++j;
        else
        { i++; j++;}
    }
    memcpy(pr+k, Set1+i-1, sizeof(int)*(SizeofSet1-i+1));
    k += SizeofSet1-i;
               
    memcpy(Res, pr, k*sizeof(int));
    *pSizeofRes = k;       
    free(pr);
       
    return 0;       
}                     
int set_union (const int Set1[], const unsigned int SizeofSet1,
                      const int Set2[], const unsigned int SizeofSet2,
                      int Res[], unsigned int* pSizeofRes)
{
    int i, j, k;
    unsigned int size_pre;   
    int *pr = 0;
       
    size_pre = SizeofSet1 + SizeofSet2;
    if ( *pSizeofRes < size_pre)
    {
        *pSizeofRes = size_pre;
        return 1;
    }
       
    pr = (int *)malloc(size_pre * sizeof(int));
    if ( pr == NULL )
       return -1;
          
    i = 0; j = 0; k = 0;
    while ( 1 )
    {
        if (i > SizeofSet1 - 1)
        {
            memcpy(pr+k, Set2+j-1, sizeof(int)*(SizeofSet2-j+1));
            k += SizeofSet2 - j;
            break;
        }
           
        if (j > SizeofSet2 - 1)
        {
            memcpy(pr+k, Set1+i-1, sizeof(int)*(SizeofSet1-i+1));
            k += SizeofSet1 - i;
            break;
        }
        if (Set1[i] < Set2[j]) pr[k] = Set1[i++];       
        else if (Set2[j] < Set1[i]) pr[k] = Set2[j++];       
        else { pr[k] = Set1[i]; ++i; ++j; }
        ++k;
    }
    memcpy(Res, pr, k*sizeof(int));
    *pSizeofRes = k;       
    free(pr);
       
    return 0;       
}                     
                         
int set_intersection (const int Set1[], const unsigned int SizeofSet1,
                      const int Set2[], const unsigned int SizeofSet2,
                      int Res[], unsigned int* pSizeofRes)
{
    int i, j, k;
    unsigned int size_pre;   
    int *pr = 0;
       
    size_pre = (SizeofSet1 > SizeofSet2) ? SizeofSet1 : SizeofSet2;
    if ( *pSizeofRes < size_pre)
    {
        *pSizeofRes = size_pre;
        return 1;
    }
       
    pr = (int *)malloc(size_pre * sizeof(int));
    if ( pr == NULL )
       return -1;
          
    i = 0; j = 0; k = 0;
    while ( i < SizeofSet1 && j < SizeofSet2 )
    {
        if (Set1[i] < Set2[j]) ++i;
        else if (Set2[j] < Set1[i]) ++j;
        else
        {
            pr[k++] = Set1[i];
            i++; j++;
        }
    }
    memcpy(Res, pr, k*sizeof(int));
    *pSizeofRes = k;       
    free(pr);
       
    return 0;   
}
void print_array(const int arr[], const size_t len)
{
    int i;
       
    for (i = 0; i < len; i++)
        printf("%d ", arr[i]);
           
    printf("
");           
}
int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}


具体忘了,利用链表可以求解,参考严蔚敏 数据结构(C语言版)

  • 鏈汉纰拌涓閬揅璇█闅鹃,瀵诲ぇ绁甯姪,鍒╃敤C璇█瀹炵幇:姹備换鎰忎袱涓泦鍚堢殑浜ら泦...
    绛旓細浠ュ墠鍐欒繃涓涓函C鐨勶紝 鐢ㄧ殑鏄暟缁勶紝妯℃嫙C++ STL閲岄潰鐨剆et_intersection锛宻et_union鍜宻et_difference鐨勫疄鐜般 绋嶄綔浜嗕慨鏀癸紝娣诲姞浜嗕簺娉ㄩ噴锛屽笇鏈涜兘甯埌浣犮傛敞鎰忥細蹇呴』鍏堝杈撳叆闆嗗悎鎺掑簭锛涜緭鍑虹粨鏋滃拰C++ STL鐨勬祴璇曠粨鏋滃惢鍚堛俰nclude <stdio.h>#include <stdlib.h>#include <string.h>int set_intersection (...
  • 涓閬揅璇█闅鹃,姹傚姪澶х
    绛旓細10 : *p=='V' ? 5 : 1;return s;}char *NumToRomanString(int n){//鏁板瓧-->缃楅┈stringint t=0,c=1000,x=2,y=5,m;char *p;m=n;while(c){for(;m>=c;t++,m-=c);c/=x,x+=y,y=x-y,x-=y;}if((p=(char *)malloc(...
  • c璇█闂,姹澶х瑙g瓟
    绛旓細char a=97 a涓哄瓧绗﹀瀷锛屽搴旂殑鏄疉SCII鐮佽〃涓崄杩涘埗鏁97瀵瑰簲鐨勫瓧绗︼紝鏄痑 c瀵瑰簲鐨勫瓧绗︽槸A锛屽搴旂殑ASCII鐮佽〃涓崄杩涘埗鏁版槸65 杈撳嚭璇彞鐨勬剰鎬濇槸璁$畻瀛楃a鍜屽瓧绗鍦ˋSCII鐮佽〃涓搴斿崄杩涘埗鏁扮殑宸苟鎸夎瀹氭牸寮忚緭鍑恒97-65=32 缁撴灉杈撳嚭a-A=32 閫C ...
  • 涓閬揅璇█闅鹃,娌℃湁鎬濊矾,璇锋暀澶х鎬庝箞鍋?
    绛旓細寰幆锛屽垵濮嬪寲锛屽彲浠ョ敤random 閫夋嫨娉曟帓搴忥紝鍙岄噸寰幆鎼炲畾锛屼笉浼氱殑璇濋殢渚挎悳涓涓嬶紝涓鍫嗘牱渚 杈撳叆鏂板 浠庡悗鍚戝墠寰幆姣旇緝 濡傛灉褰撳墠浣嶇疆鍊兼瘮鏂拌緭鍏ョ殑澶э紝閭d箞鍚庣Щ涓浣 鍚﹀垯閫鍑哄惊鐜 閫鍑轰綅缃殑涓嬩竴涓綅缃氨鏄彃鍏ョ偣 澶ц嚧灏辨槸 for(i=N-2;a[i]>v &&i>=0;i--)a[i+1]=a[i];a[i+1]=v;...
  • 涓閬揷璇█鎸囬拡棰,姹澶х瑙g瓟,鎰熻阿
    绛旓細杩欓鐩緭鍑虹殑缁撴灉鏄 5锛6锛6瑙f瀽锛氶鍏堝0鏄庝竴涓暣鍨嬫暟缁刟锛屾暣褰㈠彉閲弝锛屾暣鍨嬫寚閽坧銆俻鎸囧悜鏁扮粍a[]鐨勭1涓厓绱狅紝涔熷氨鏄8銆傛帴涓嬫潵锛屾寚閽坧鍏--锛屾寚鍚戜簡鏁扮粍a[]鐨勭0涓厓绱狅紝涔熷氨鏄5銆備箣鍚巠鍙栧嚭p鎸囧悜鐨勫唴瀹癸紝鍗5銆備箣鍚巔鎸囬拡鎸囧悜鐨勫唴瀹瑰張绱姞锛屽嵆鏁扮粍a[]鐨勭0涓厓绱犱粠5鍙樻垚浜6銆傚洜姝ゆ墦鍗拌緭鍑簓鐨...
  • C璇█缂栫▼闅鹃,璺眰澶х瑙g瓟
    绛旓細int o, n, e;int sum = 0;for (o = 0; o <= 9; o++) {for (n = 0; n <= 9; n++) {if (n == o)continue;for (e = 0; e <= 9; e++) {if (e == n)continue;int temp1 = o * 100 + n * 10 + e;int temp = temp1 + temp1;int w = temp / 10 % ...
  • C璇█!鏈汉鏂版墜鑷閬囧埌浜嗕竴涓闅鹃!璇澶х浠府甯繖,绁濅綘浠柊骞村揩涔,缇 ...
    绛旓細include <stdio.h>int main(void){ int i, j; printf("璇疯緭鍏ヤ竴涓暣鏁:"); scanf("%d", &i); j=1; while(j*=2 < i) printf("%7d", j); return 0;} 闄: 杩欎釜绋嬪簭鐢╠o...while浼氬彂鐢熼敊璇.
  • 涓閬揷璇█鐨勯棶棰,姹澶х瑙g瓟
    绛旓細绗竴涓猰ax鏈夌己鐪佸弬鏁癱锛岃繖涓弬鏁板彲鍐欏彲涓嶅啓锛屽洜姝ax(3,4)鏃犳硶鍒ゆ柇璋冪敤鍝竴涓猰ax锛屽嚭鐜颁簩涔夋э紝閿欒锛堝鏋滄槸C璇█锛閭g洿鎺ュ氨涓嶆敮鎸佸嚱鏁扮殑閲嶈浇锛屽悓鍚嶅嚱鏁扮洿鎺ヤ細鍑洪敊锛夊浜庝换浣曞嚱鏁版潵璇达紝鍙傛暟鐨勭己鐪佸彧鑳芥槸鍙宠竟锛岃璋冪敤ferror锛屽彲浠ヤ娇ferror()锛宖error(1)锛宖error(1,2锛,浣嗙粷涓嶈兘缂虹渷宸﹁竟鐨勫弬鏁 ...
  • 姹澶х瑙g瓟涓閬揅璇█棰榽钀屾柊瀹炲湪涓嶄細~璺眰澶х!
    绛旓細int n){printf("%d\t%s\t",class1[n].no,class1.name); for(i=0;i<5;i++) printf("%.1f%c",class1[n].score[i],i<4?'\t','\n');}void printstudent(int n){float s=0; for(i=0;i<5;i++) s+=class1[n].score[i]; printf("%.1f\n",s/5);} ...
  • 涓閬揷璇█缂栫▼棰,鎵句笉鍒伴敊澶,姹澶х
    绛旓細for (j = 0; j <N - 1; j++)杩欎釜璇彞鐨勯棶棰橈紝褰撳瓧绗︿覆闀垮害娌℃湁閭d箞澶ф椂灏辨尓鍒颁簡鏈鍚庡幓浜嗐傛敼鎴恌or (j = 0; j < strlen(w) - 1; j++)灏卞彲浠ヤ簡銆傚疄闄呬笂杩樻湁涓绉嶆洿绠鍗曠殑瑙e喅鏂规硶锛屼娇鐢╩emcpy鐩存帴鎷疯礉鍐呭瓨銆倂oid fun(char* w, int m){int len = strlen(w);char* tmp = (char...
  • 扩展阅读:夏日必备清凉好物 ... 扫一扫题目出答案 ... 扫一扫搜答案免费 ... 古董鉴定扫一扫 ... 扫一扫自动答题 ... 史上最难智商题 ... 免费搜题神器 ... 初中数学最难的一道题 ... 数学最诡异的一题 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网