本文介绍了两个数组的交集 - Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,当


SIZEOF_A = 6
SIZEOF_B = 4
集合A = {0,1,2,3,4,5,6}
集合B = {1,10,11,12}

交集的所有扭曲( 1048832 )。
这是为什么?


的#include" usefunc.h"/ *#定义SIZEOF_A 2
#定义SIZEOF_B 4 * /的typedef枚举{
        A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y, ž
}设置;无效dispSet(组号码[],诠释size_numbers){
  INT I;
  的printf(QUOT; [&QUOT);
  对于(i = 0; I< size_numbers-1;我++){
    的printf(QUOT;%D,&QUOT ;,号[I]);
  }
  的printf(QUOT;%d个] QUOT ;,号码[size_numbers-1]);
  的printf(QUOT; \\ n");
}INT in_arr(INT A,集合B [],诠释B_size){
    INT RES = 0;
    INT计数器;
    对于(计数器= 0;反< B_size;反++){
        若(a == B〔计数器]){
            RES = 1;
            打破;
        }
    }
    返回水库;
}INT arr_in_arr(设定较小的[],INT smaller_size,设置更大的[],诠释bigger_size){
    中期业绩= 1;
    INT计数器;
    对于(计数器= 0;反< smaller_size;反++){
        如果(in_arr(小[计数器],做大,bigger_size)){
            继续;
        }
        其他{
            RES = 0;
            打破;
        }
    }
    返回水库;
}INT size_c(设置ARR1 [],INT arr1_size,设置ARR2 [],诠释arr2_size){
    INT I;
    INT newsize = 0;
    对于(i = 0; I< arr1_size;我++){
        如果(!in_arr(ARR1 [I],ARR2,arr2_size)){
            newsize ++;
        }
    }
    对于(i = 0; I< arr2_size;我++)newsize ++;
    返回newsize;
}INT size_d(设置ARR1 [],INT arr1_size,设置ARR2 [],诠释arr2_size){
    INT I;
    INT SIZEOF_D = 0;
    对于(I = 0; I&下; arr1_size;我++)如果(in_arr(ARR1 [I],ARR2,arr2_size))SIZEOF_D ++;
    返回SIZEOF_D;
}INT加入(设置ARR1 [],INT arr1_size,设置ARR2 [],INT arr2_size,设置ARR3 [],诠释arr3_size){
    INT I,J = 0;
    对于(i = 0; I< arr1_size;我++)ARR3 [I] = ARR1 [I]    对于(i = 0; I< arr2_size;我++){
        如果(!in_arr(ARR2 [I],ARR3,arr3_size)){
            ARR3 [J + arr1_size] = ARR2 [I]
            J ++;
        }
    }
}INT交集(设置ARR1 [],INT arr1_size,设置ARR2 [],INT arr2_size,设置ARR3 [],诠释arr3_size){
    INT I,J = 0;
    对于(i = 0; I< arr1_size;我++){
        如果(in_arr(ARR1 [I],ARR2,arr2_size))ARR3 [J] = ARR1 [I];
        J ++;
    }
}INT GETSET(ARR设置[],INT大小){
    INT I;
    的printf(QUOT;具有= 0开始,进入对应于一个字母一个数字\\ n"。);
    对于(i = 0; I<大小;我++){
        的printf(QUOT;民:");
        改编[I] = GetInteger();
    }
}诠释主(){
    的printf(QUOT; A&QUOT的大小;); INT SIZEOF_A = GetInteger();
    的printf(QUOT; b的大小和QUOT;?); INT SIZEOF_B = GetInteger();
    设A [SIZEOF_A];的printf(QUOT; \\ NA:\\ n"); GETSET(A,SIZEOF_A);
    集合B [SIZEOF_B]的printf(" B:\\ n"); GETSET(B,SIZEOF_B);
    INT SIZEOF_C = size_c(A,SIZEOF_A,B,SIZEOF_B);
    INT SIZEOF_D = size_d(A,SIZEOF_A,B,SIZEOF_B);
    的printf(QUOT; d尺寸:%D&QUOT ;, SIZEOF_D);
    设置C [SIZEOF_C];设置D [SIZEOF_D];
    加入(A,SIZEOF_A,B,SIZEOF_B,C,SIZEOF_C);
    交集(A,SIZEOF_A,B,SIZEOF_B,D,SIZEOF_D);
    的printf(QUOT; \\ n有关集合\\ n");
    的printf(QUOT; A:"); dispSet(A,SIZEOF_A);
    的printf(" B:"); dispSet(B,SIZEOF_B);
    的printf(QUOT; \\ n");的printf(QUOT;工会A,B:"); dispSet(C,SIZEOF_C);
    的printf(QUOT;交点A,B:"); dispSet(D,SIZEOF_D);
    的printf(QUOT;%S \\ n \\ n",(arr_in_arr(A,SIZEOF_A,B,SIZEOF_B)== 1)" B包含A":" B不包含A");
}


解决方案

在十字路口,你的if语句应该是:

 如果(in_arr(ARR1 [I],ARR2,arr2_size)){
    ARR3 [J] = ARR1 [I]
    J ++;
}

除非你找到一个匹配,不要增加学家

for some reason, when

SIZEOF_A = 6
SIZEOF_B = 4
set A = {0, 1, 2, 3, 4, 5, 6}
set B = {1, 10, 11, 12}

the intersection is all screwy (1048832).why is this?

#include "usefunc.h"

/* #define SIZEOF_A 2
#define SIZEOF_B 4 */

typedef enum {
        a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
} set;

void dispSet(set numbers[], int size_numbers) {
  int i;
  printf("[ ");
  for (i = 0; i < size_numbers-1; i++) {
    printf("%d, ", numbers[i]);
  }
  printf("%d ]", numbers[size_numbers-1]);
  printf("\n");
}

int in_arr(int A, set B[], int B_size) {
    int res = 0;
    int counter;
    for (counter = 0; counter < B_size; counter++) {
        if (A == B[counter]) {
            res = 1;
            break;
        }
    }
    return res;
}

int arr_in_arr(set smaller[], int smaller_size, set bigger[], int bigger_size) {
    int res = 1;
    int counter;
    for (counter = 0; counter < smaller_size; counter++) {
        if (in_arr(smaller[counter], bigger, bigger_size)) {
            continue;
        }
        else {
            res = 0;
            break;
        }
    }
    return res;
}

int size_c(set arr1[], int arr1_size, set arr2[], int arr2_size) {
    int i;
    int newsize = 0;
    for (i = 0; i < arr1_size; i++) {
        if (!in_arr(arr1[i], arr2, arr2_size)) {
            newsize++;
        }
    }
    for (i = 0; i < arr2_size; i++) newsize++;
    return newsize;
}

int size_d(set arr1[], int arr1_size, set arr2[], int arr2_size) {
    int i;
    int SIZEOF_D = 0;
    for (i = 0; i < arr1_size; i++) if (in_arr(arr1[i], arr2, arr2_size)) SIZEOF_D++;
    return SIZEOF_D;
}

int Join(set arr1[], int arr1_size, set arr2[], int arr2_size, set arr3[], int arr3_size) {
    int i, j = 0;
    for (i = 0; i < arr1_size; i++) arr3[i] = arr1[i];

    for (i = 0; i < arr2_size; i++) {
        if (!in_arr(arr2[i], arr3, arr3_size)) {
            arr3[j+arr1_size] = arr2[i];
            j++;
        }
    }
}

int Intersection(set arr1[], int arr1_size, set arr2[], int arr2_size, set arr3[], int arr3_size) {
    int i, j = 0;
    for (i = 0; i < arr1_size; i++) {
        if (in_arr(arr1[i], arr2, arr2_size)) arr3[j] = arr1[i];
        j++;
    }
}

int getSet(set arr[], int size) {
    int i;
    printf("Starting with a=0, enter a number that corresponds to a letter.\n");
    for (i = 0; i < size; i++) {
        printf("NUM: ");
        arr[i] = GetInteger();
    }
}

int main () {
    printf("size of a? "); int SIZEOF_A = GetInteger();
    printf("size of b? "); int SIZEOF_B = GetInteger();
    set A[SIZEOF_A]; printf("\nA:\n"); getSet(A, SIZEOF_A);
    set B[SIZEOF_B]; printf("B:\n"); getSet(B, SIZEOF_B);
    int SIZEOF_C = size_c(A, SIZEOF_A, B, SIZEOF_B);
    int SIZEOF_D = size_d(A, SIZEOF_A, B, SIZEOF_B);
    printf("size of D: %d", SIZEOF_D);
    set C[SIZEOF_C]; set D[SIZEOF_D];
    Join(A, SIZEOF_A, B, SIZEOF_B, C, SIZEOF_C);
    Intersection(A, SIZEOF_A, B, SIZEOF_B, D, SIZEOF_D);
    printf("\nFor the sets\n");
    printf("A: "); dispSet(A, SIZEOF_A);
    printf("B: "); dispSet(B, SIZEOF_B);
    printf("\n"); printf("union A,B: "); dispSet(C, SIZEOF_C);
    printf("intersection A,B: "); dispSet(D, SIZEOF_D);
    printf("%s\n\n", (arr_in_arr(A, SIZEOF_A, B, SIZEOF_B) == 1)?"B contains A":"B does not contain A");
}
解决方案

In Intersection, your if statement should be:

if (in_arr(arr1[i], arr2, arr2_size)) {
    arr3[j] = arr1[i];
    j++;
}

Don't increment j unless you find a match.

这篇关于两个数组的交集 - Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:48