分配可变数量的变量对象的所有组合

时间:2012-05-25 05:26:45

标签: c recursion

我遇到这个递归问题有困难。我以为我有一个答案,但它不起作用,我根本不知道为什么,所以我想我会问专家。请放轻松我,15年前我接受了C编程,即使那时我也许是B学生。我不懂C ++或Java。

目的是生成从0到(n [j] -1)的所有可能的整数组合,其中j可以是任意整数。现在它被硬编码为2,但我希望它能够最终获得任何价值。

无论如何,这是我的代码。在此先感谢您的帮助。

编辑: 对于下面的代码,我定义了2个序列,第0个序列的长度为2(0,1),第1个序列的长度为3(0,1,2)。 所需的输出如下:

p[0][0] = 0
p[0][1] = 0
p[1][0] = 0
p[1][1] = 1
p[2][0] = 0
p[2][1] = 2
p[3][0] = 1
p[3][1] = 0
p[4][0] = 1
p[4][1] = 1
p[5][0] = 1
p[5][1] = 2

即,

  • 第0个组合从序列0贡献0,从序列1贡献0
  • 第一组合从序列0贡献0,从序列1贡献1
  • 第二组合从序列0贡献0,从序列1贡献2
  • 第3组合从序列0贡献1,从序列1贡献0
  • 第4组合从序列0贡献1,从序列1贡献1
  • 第5组合从序列0贡献1,从序列1贡献2

我希望这能让我更清楚我要做的事情!

#include <stdio.h>
#include <stdlib.h>

int recurse (int **p, int *n, int nclass, int classcount, int combcount);

int recurse (int **p, int *n, int nclass, int classcount, int combcount)
{
  int k, j, kmax;
  kmax = n[classcount];
  j = classcount;

  if (j == nclass)  {
    return (combcount+1);
  }

  for (k = 0; k < kmax; k++)  {
    p[combcount][j] = k;
    combcount = recurse (p, n, nclass, j+1, combcount);
  }
}

int main (void)
{
  int **p, n[2], i, j;

  n[0] = 2;
  n[1] = 3;

  p = (int **) malloc ((n[0]*n[1]) * sizeof (int *));
  for (i = 0; i < (n[0]*n[1]); i++)  {
    p[i] = (int *) malloc (2 * sizeof (int));
    for (j = 0; j < 2; j++)
      p[i][j] = -1;
  }

/* p[i][j] = the value of the integer in the ith combination
   arising from the sequence 0...n[j]-1 */

  recurse (p, n, 2, 0, 0);

  for (i = 0; i < (n[0]*n[1]); i++)
    for (j = 0; j < 2; j++)
      printf ("%d %d: %d\n", i, j, p[i][j]);

  for (i = 0; i < (n[0]*n[1]); i++)
    free (p[i]);
  free (p);
  return (0);
}

2 个答案:

答案 0 :(得分:0)

#include <stdio.h>
#include <stdlib.h>

void recurse(int *n, int *accum, int **p, int N, int k) {
    static int comb;
    int i, j;
    if (k == 0)
        comb = 0;
    if (k == N) {
        for (i = 0; i < N; ++i)
            p[comb][i] = accum[i];
        comb++;
    }
    else
        for (i = 0; i < n[k]; ++i) {
            accum[k] = i;
            recurse(n, accum, p, N, k+1);
        }
}

int main(void) {
    const int N = 2;
    int n[N];
    int accum[N];
    int **p;
    int mult;
    int i, j;
    n[0] = 2;
    n[1] = 3;
    for (mult = 1, i = 0; i < N; mult *= n[i], ++i);
    p = malloc(mult*sizeof(int*));
    for (i = 0; i < mult; i++)
        p[i] = malloc(N*sizeof(int));
    recurse(n, accum, p, N, 0);
    for (i = 0; i < mult; ++i)
        for (j = 0; j < N; ++j)
            printf("p[%d][%d] = %d\n", i, j, p[i][j]);
    for (i = 0; i < mult; i++)
        free(p[i]);
    free(p);
}

答案 1 :(得分:0)

#include <stdio.h>
#include <stdlib.h>

int recurse (int **p, int *n, int nclass, int classcount, int p_size){
    int i, j, jmax, k, kmax;

    if (classcount == nclass) return 1;

    i = 0;
    kmax = n[classcount];
    while(i < p_size){
        for (k = 0; k < kmax; ++k){
            jmax = recurse (p, n, nclass, classcount+1, p_size);
            for(j = 0;j < jmax; ++j)
                p[i++][classcount] = k;
        }
    }
    return kmax*jmax;
}

int main (void){
    int **p, n[2], i, j;
    int sizeAll, sizeN;

    n[0] = 2;
    n[1] = 3;
    sizeAll = n[0]*n[1];
    sizeN = sizeof(n)/sizeof(int);
    p = (int **) malloc (sizeAll * sizeof (int *));
    for (i = 0; i < sizeAll; ++i) {
        p[i] = (int *) malloc (sizeN * sizeof (int));
        for (j = 0; j < sizeN; ++j)
            p[i][j] = -1;
    }

    recurse (p, n, sizeN, 0, sizeAll);

    for (i = 0; i < sizeAll; ++i)
        for (j = 0; j < sizeN; ++j)
            printf ("%d %d: %d\n", i, j, p[i][j]);

    for (i = 0; i < sizeAll; ++i)
        free (p[i]);
    free (p);
    return (0);
}