Binary c程序中的二进制数

Binary c程序中的二进制数,binary,Binary,我正在尝试编写一个程序,提示用户输入一个正整数N并打印长度为N的所有二进制字符串集。例如,如果用户输入3,程序将打印: 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 我已经尝试了一段时间,但我无法做到这一点。如果有人能帮我,我将不胜感激!!感谢这个问题是一个典型的算法回溯问题,我在这里用C语言编写了示例代码,您可以参考 #include <stdio.h> #include <stdlib.h> void getAll

我正在尝试编写一个程序,提示用户输入一个正整数N并打印长度为N的所有二进制字符串集。例如,如果用户输入3,程序将打印:

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

我已经尝试了一段时间,但我无法做到这一点。如果有人能帮我,我将不胜感激!!感谢

这个问题是一个典型的算法回溯问题,我在这里用C语言编写了示例代码,您可以参考

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

void getAllBi(int depth, int A[], int n) {
    if(depth >= n) {
        int i;
        for(i = 0; i < n; ++i) {
            printf("%d ", A[i]);
        }
        printf("\n");
        return;
    }
    A[depth] = 0;
    getAllBi(depth+1, A, n);
    A[depth] = 1;
    getAllBi(depth+1, A, n);
}

int main() {
    int n;
    scanf("%d", &n);
    int *A = (int*)malloc(n * sizeof(int));

    getAllBi(0, A, n);
    return 0;
}
#包括
#包括
void getAllBi(整数深度,整数A[],整数n){
如果(深度>=n){
int i;
对于(i=0;i
您有什么特别的问题?堆栈溢出不是编码服务。