Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 求两组10位数字的并集_C - Fatal编程技术网

C 求两组10位数字的并集

C 求两组10位数字的并集,c,C,我试图找到两组10位数字的并集,我传递三个int数组:第一,第二,comp,这将保存并集 到目前为止,我已经在一个数组中添加了第一个和第二个。我在考虑在comp[]中找到匹配的索引,然后通过删除它们进行过滤。我想有一个更简单的方法。谁能给我一个提示吗 基本上我要 first[] = [1,2,3,4,5,6,7,8,9,10]; second[] = [1,2,3,4,11,12,13,14,15,16]; 我想回来 comp[] = [5,6,7,8,9,10,11,12,13,14,15,

我试图找到两组10位数字的并集,我传递三个int数组:第一,第二,comp,这将保存并集

到目前为止,我已经在一个数组中添加了第一个和第二个。我在考虑在comp[]中找到匹配的索引,然后通过删除它们进行过滤。我想有一个更简单的方法。谁能给我一个提示吗

基本上我要

first[] = [1,2,3,4,5,6,7,8,9,10];
second[] = [1,2,3,4,11,12,13,14,15,16];
我想回来

comp[] = [5,6,7,8,9,10,11,12,13,14,15,16];
数字不一定是有序的

int compound(int first[],int second[],int comp[]){
int i=0;
int indicies[20];
for(int j = 0; j<SIZE; j++){
    comp[i]=first[j];
    i++;
}

for(int k = 0; k<SIZE; k++){
    comp[i]=second[k];
    i++;
}
int z=0;
for(int l = 0; l<SIZE*2; l++){
    for(int m = 0; m<SIZE*2; m++){
        if(comp[l]==comp[m]){
            indicies[z]=m;
            z++;
        }}}


return 0;
}

好的第一步几乎总是分类

对两个输入集进行排序,除非您知道它们已经排序

然后一次迭代两个索引,只将满足条件的元素添加到输出中,这些元素似乎是并集减去交集,因此只在一个索引中


奖励:输出集将被排序。

好的第一步几乎总是排序

对两个输入集进行排序,除非您知道它们已经排序

然后一次迭代两个索引,只将满足条件的元素添加到输出中,这些元素似乎是并集减去交集,因此只在一个索引中


好处:输出集将被排序。

我建议您从编写containsint[],int方法开始,如

#include <stdio.h>
#include <stdbool.h>

bool contains(int arr[], int val) {
  int offset;
  for (offset = 0; arr[offset] != '\0'; offset++) {
    if (arr[offset] == val) return true;
  }
  return false;
}
最后,您可以像这样测试它

int main(int argc, char *args[]) {
  int a[] = {1,2,3,'\0'};
  int b[] = {2,3,4,'\0'};
  int c[3];
  int count = compound(a,b,c);
  int i;
  for (i = 0; i < count; i++) {
    printf("%i\n", c[i]);
  }
}

我建议您首先编写containsint[],int方法,如

#include <stdio.h>
#include <stdbool.h>

bool contains(int arr[], int val) {
  int offset;
  for (offset = 0; arr[offset] != '\0'; offset++) {
    if (arr[offset] == val) return true;
  }
  return false;
}
最后,您可以像这样测试它

int main(int argc, char *args[]) {
  int a[] = {1,2,3,'\0'};
  int b[] = {2,3,4,'\0'};
  int c[3];
  int count = compound(a,b,c);
  int i;
  for (i = 0; i < count; i++) {
    printf("%i\n", c[i]);
  }
}

如果数值范围很小,可以执行以下操作:

#include <stdio.h>

#define MAX 20  // small numeric range

#define sz(a) (sizeof(a)/sizeof(*(a)))

int xunion(int *a, int sa, int *b, int sb, int *c) {
  int n[MAX] = {0};
  for (int i=0; i<sa; i++) n[a[i]] = 1;
  for (int i=0; i<sb; i++) n[b[i]] = 1;
  int j=0;
  for (int i=0; i<MAX; i++) if (n[i]) c[j++] = i;
  return j;
}

void prn(int *a, int s) {
  while (s-- > 0) printf("%d ", *a++);
  putchar('\n');
}

int main() {
  int a[] = {6, 3, 4, 7, 5};
  int b[] = {2, 4, 5, 7, 6, 3};
  int c[MAX];
  prn(a, sz(a));
  prn(b, sz(b));
  int n = xunion(a, sz(a), b, sz(b), c);
  prn(c, n);
  return 0;
}

如果数值范围很小,可以执行以下操作:

#include <stdio.h>

#define MAX 20  // small numeric range

#define sz(a) (sizeof(a)/sizeof(*(a)))

int xunion(int *a, int sa, int *b, int sb, int *c) {
  int n[MAX] = {0};
  for (int i=0; i<sa; i++) n[a[i]] = 1;
  for (int i=0; i<sb; i++) n[b[i]] = 1;
  int j=0;
  for (int i=0; i<MAX; i++) if (n[i]) c[j++] = i;
  return j;
}

void prn(int *a, int s) {
  while (s-- > 0) printf("%d ", *a++);
  putchar('\n');
}

int main() {
  int a[] = {6, 3, 4, 7, 5};
  int b[] = {2, 4, 5, 7, 6, 3};
  int c[MAX];
  prn(a, sz(a));
  prn(b, sz(b));
  int n = xunion(a, sz(a), b, sz(b), c);
  prn(c, n);
  return 0;
}

这不是并集减去交集吗?你熟悉a的概念吗?你想如何传达结果有多大?这不是并集减去交集吗?你熟悉a的概念吗?你想如何传达结果有多大?你的c数组太小,无法容纳结果1,2,3,4。非常感谢,巴德。stdbool.h不是C99的特性吗?如果是这样的话,您可以在for…@Jack的第一个参数中声明偏移量,但在我的平台上没有gcc标志。您的c数组太小,无法容纳结果1,2,3,4.5。非常感谢,巴德。stdbool.h不是C99的特性吗?若是这样的话,你们可以在for…@Jack的第一个参数中声明偏移量,但在我的平台上并没有gcc的标志。我甚至不知道这里发生了什么。haha@BillTudor数组n被初始化为零。两个输入数组都循环通过,将n中的对应元素设置为1。然后循环n,只要有1,索引就是原始列表中的一个数字,所以它被附加到输出数组中。n可以是字节数组,甚至是位数组。我不知道这里发生了什么。haha@BillTudor数组n被初始化为零。两个输入数组都循环通过,将n中的对应元素设置为1。然后循环n,只要有1,索引就是原始列表中的一个数字,所以它被附加到输出数组中。n可以是字节数组,甚至是位数组。