在n个二维数组中进行c-搜索



嗨我需要以下逻辑来实现(二维的(n个阵列。这里我考虑了一维的3个阵列

#include<stdio.h>
main()
{
    int a[4]={2,1,4,7},b[4]={3,-3,-8,0},c[4]={-1,-4,-7,6},sum,i,j,k,val=0;
    for(i=0;i<4;i++) {
        for(j=0;j<4;j++) {
            for(k=0;k<4;k++) {
                sum = a[i]+b[j]+c[k];
                if(sum == val)
                printf("%d  %d  %dn", a[i], b[j], c[k]);
            }
        }
    }

}

输出:2-8 61 3-41 0-14 3-74-3-14 0-47-3-47 0-7

有关语法信息,请参阅维基百科中的C语法。

在实践中,您需要使用int array[3][4]=。。。以创建具有3行4列的阵列。稍后的代码将对当前a、b和c数组的访问替换为针对每种情况的固定行索引。

其余的实现只是一个练习,因为这听起来像是我的家庭作业。

好问题:-(
我不会公布我的全部解决方案,因为这个问题似乎是家庭作业。只是一些提示。。。

我用递归解决了这个问题:我使用的简化过程是n数组中查找target的和与在n-1数组中查找target - ONE_ELEMENT的和相同

使用您的3个阵列和零目标的示例

find 3 elements with sum 0           in {2, 1, 4, 7}, {3, -3, -8, 0}, {-1, -4, -7, 6}
find 2 elements with sum 0 - 2 (-2)  in {3, -3, -8, 0}, {-1, -4, -7, 6}
find 1 elements with sum -2 - 3 (-5) in {-1, -4, -7, 6}              NOT FOUND
find 1 elements with sum -2 - -3 (1) in {-1, -4, -7, 6}              NOT FOUND
find 1 elements with sum -2 - -8 (6) in {-1, -4, -7, 6}              YAY! FOUND
...

为了使它更容易工作,我为数组创建了一个数据结构,并想出了一种在递归函数的几次调用之间传递信息的方法(我使用了在helper递归设置函数中分配的另一个数组(。

阵列的结构是

struct sizedarray {
  int *data;
  size_t nelems;
};

递归函数和辅助函数的原型是

findtarget(int target, struct sizedarray *arrays, size_t narrays);
findtarget_recursive(int target, struct sizedarray *arrays, size_t narrays, size_t level, int *saved);

编辑以添加工作解决方案

#include <stdio.h>
#include <stdlib.h>
/* struct to hold arrays with varying sizes */
struct sizedarray {
  int *data;
  size_t nelems;
};
void findtarget_recursive(int target,
                         struct sizedarray *arrays,
                         size_t narrays,
                         size_t level,
                         int *saved) {
  size_t k, j;
  struct sizedarray *curarray = arrays + level;
  /* if no arrays left to search return */
  if (level == narrays) {
    return;
  }
  /* if only 1 arrays do not recurse */
  if (level + 1 == narrays) {
    for (k = 0; k < curarray->nelems; k++) {
      if (curarray->data[k] == target) {
        /* print saved elements from previous arrays */
        for (j = 0; j < level; j++) {
          printf("%d ", saved[j]);
        }
        /* print current element from current array */
        printf("%dn", curarray->data[k]);
      }
    }
    return;
  } else {
    /* when 2 or more arrays left, recurse */
    for (k = 0; k < curarray->nelems; k++) {
      saved[level] = curarray->data[k];
      findtarget_recursive(target - curarray->data[k],
                           arrays,
                           narrays,
                           level + 1,
                           saved);
    }
  }
}
int findtarget(int target, struct sizedarray *arrays, size_t narrays) {
  int *saved = NULL;
  saved = malloc(narrays * sizeof *saved);
  /* assume it worked, needs something when it fails */
  if (saved) {
    findtarget_recursive(target, arrays, narrays, 0, saved);
    free(saved);
  }
  return 0;
}
int main(void) {
  int a0[] = {2, 1, 4, 7};
  int a1[] = {3, -3, -8, 0};
  int a2[] = {-1, -4, -7, 6};
  int a3[] = {1, 5, 6, 7};
  int a4[] = {-10, -4, -1, 3, 8};
  int a5[] = {17, 18, 19, 20, 21, 22, 23, 24, 25};
  struct sizedarray arrays[6];
  int target = 0;
  arrays[0].data = a0; arrays[0].nelems = sizeof a0/sizeof *a0;
  arrays[1].data = a1; arrays[1].nelems = sizeof a1/sizeof *a1;
  arrays[2].data = a2; arrays[2].nelems = sizeof a2/sizeof *a2;
  findtarget(target, arrays, 3);
  arrays[3].data = a3; arrays[3].nelems = sizeof a3/sizeof *a3;
  arrays[4].data = a4; arrays[4].nelems = sizeof a4/sizeof *a4;
  arrays[5].data = a5; arrays[5].nelems = sizeof a5/sizeof *a5;
  puts("nnwith 6 arrays ...");
  findtarget(target, arrays, 6);
  return 0;
}

相关内容

  • 没有找到相关文章

最新更新