二维数组 C 中的布尔表

Andrei Mădălin Oancă

我在创建以下数组时遇到了一些困难。我的任务是使用递归填充 2D 数组,其中 0 和 1 的所有可能组合按词法顺序取 m 次。从数学上讲,有 2 ^ m 种组合。我的程序只是用相同的顺序 0 1 0 1 填充数组的前 3 行,然后只打印其余的行 0 0 0 0。

示例 m=4

0   0   0   0
0   0   0   1   
0   0   1   0   
0   0   1   1   
0   1   0   0   
0   1   0   1   
0   1   1   0   
0   1   1   1   
1   0   0   0   
1   0   0   1   
1   0   1   0   
1   0   1   1   
1   1   0   0   
1   1   0   1   
1   1   1   0   
1   1   1   1

到目前为止,这是我的代码,如果有人可以纠正它并解释我做错了什么,我很感激,因为我自己无法发现错误

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

void *safeMalloc(int n) {
    void *p = malloc(n);
    if (p == NULL) {
        printf("Error: malloc(%d) failed. Out of memory?\n", n);
        exit(EXIT_FAILURE);
    }
    return p;
}


void combine(int** arrTF,int m,int n,int row,int col){
    if(m==0){
        if(row<pow(2,m)){
            row++;
            combine(arrTF,n,n,row,0);
        }else{
            return;
        }
    }else{
        arrTF[row][col]=0;
        col++;
        combine(arrTF,m-1,n,row,col);

        arrTF[row][col]=1;
        col++;
        combine(arrTF,m-1,n,row,col);
    }

}



int main(int argc, char *argv[]) {
    int m
    scanf("%d",&m);


    int** arrTF;

    arrTF = safeMalloc(pow(2,m)*sizeof(int *));
    for (int r=0; r < pow(2,m); r++) {
        arrTF[r] = safeMalloc(m*sizeof(int));
    }


    for(int i=0;i<pow(2,m);i++){
        for(int j=0;j<m;j++){
            arrTF[i][j]=0;
        }
    }

    combine(arrTF,m,m,0,0);

    for(int i=0;i<pow(2,m);i++){
        for(int j=0;j<m;j++){
            printf("%d ",arrTF[i][j]);
        }
        printf("\n");
    }

    return 0;
}
HS

你想要所有可能(2^m)的组合0's1's采取的m词汇顺序时间和你正在使用一个二维数组来存储结果。如果您只想打印 和 的所有可能组合,0's1's不是稍后将其存储在二维数组和打印数组中,那么事情将非常容易

存储的组合0's,并1's以二维数组是有点棘手,因为每个组合的二维数组的一个元素。你要生成的组合0's1's按照递归算法。因此,假设您的算法在某个阶段生成组合0010该组合存储在二维数组的元素中。下一个组合将是0011,递归算法将通过将最后一个组合 ( 0010 )中的最后一个数字从0更改1来生成该组合

因此,这意味着每次生成组合时,您都需要将该组合复制到其在 2D 数组中的连续位置。例如,如果在算法开始计算下一个组合之前将0010存储在二维数组的索引 2 处,我们需要做两件事:

  1. 将索引 2 的元素复制到索引 3
  2. 增加行号,使最后一个组合完好无损

(比如说,这是二维数组)

|0|0|0|0| 索引 0

|0|0|0|1| 索引 1

|0|0|1|0| 索引 2 ---> 将此复制到其连续位置(即在索引 3 处)

|0|0|1|1| 索引 3 ---> 最后一个组合(索引 2),最后一位数字从 0 变为 1

.....

.....

.....

我们需要在生成每个组合后执行此操作。现在,我希望你知道你犯的错误。

很少有好的做法可以遵循:

  1. 如果要分配内存并用 0 初始化它,请使用calloc代替malloc

  2. 对于同一输入一次又一次调用的任何数学函数,最好调用一次并将结果存储在变量中,并在需要时使用该结果。

  3. 不要包含程序中不需要的任何头文件。

  4. 完成后,确保释放程序中动态分配的内存。

我在你的程序中做了更正:

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

void *safeMalloc(size_t n, size_t size) {
    void *p = calloc(n, size);
    if (p == NULL) {
        printf("Error: calloc(%zu) failed. Out of memory!\n", n);
        exit(EXIT_FAILURE);
    }
    return p;
}

void deallocate(int ** ptr, int row) {
    for(int i = 0; i<row; i++)
            free(ptr[i]);
    free(ptr);
}

void combine(int **arrTF, int m, int max_col, int max_row) {
    static int row;
    if(m==0){
        int i;
        if (row<(max_row - 1))
        {
            for(i=0; i<max_col; i++)
                arrTF[row+1][i] = arrTF[row][i];
        }
        row++;
        return;
    } else {
        arrTF[row][max_col-m] = 0;
        combine(arrTF, m-1, max_col, max_row);

        arrTF[row][max_col-m] = 1;
        combine(arrTF, m-1, max_col, max_row);
    }
}

int main(int argc, char *argv[]) {
    int** arrTF;
    int m, max_row;

    printf ("Enter number: \n");
    scanf("%d", &m);

    max_row = pow(2, m);

    arrTF = safeMalloc(max_row, sizeof(int *));
    for (int r=0; r<max_row; r++) {
        arrTF[r] = safeMalloc(m, sizeof(int));
    }

    combine(arrTF, m, m, max_row);

    for(int i=0; i<max_row; i++) {
        for(int j=0; j<m; j++) {
            printf("%d ", arrTF[i][j]);
        }
        printf("\n");
    }
    deallocate(arrTF, max_row);
    return 0;
}

输出:

$ ./a.out
Enter number: 
2
0 0 
0 1 
1 0 
1 1 

$ ./a.out
4
0 0 0 0 
0 0 0 1 
0 0 1 0 
0 0 1 1 
0 1 0 0 
0 1 0 1 
0 1 1 0 
0 1 1 1 
1 0 0 0 
1 0 0 1 
1 0 1 0 
1 0 1 1 
1 1 0 0 
1 1 0 1 
1 1 1 0 
1 1 1 1 

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章