c++ - 循环递归(生成数据)

标签 c++ c recursion

你好,我想知道是否可以通过递归来做这样的事情,怎么做? 我希望能够选择我想要的循环数,例如 GenerateNumbers(x),其中 x 是我内部的循环数。

int a, b, c;
for (a = 0; a < 10; a++)
{
    printf("\n%d", a);
    for (b = 0; b < 10; b++)
    {
        printf("\n%d%d", a, b);
        for (c = 0; c < 10; c++)
        {
            printf("\n%d%d%d", a, b, c);
        }
    }
}

最佳答案

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



int GenerateNumbersHelper(int depth,int max_depth,int* data)
{
    int i;
    if(depth == 1 + max_depth)
        return 0;

    for(i=0;i<depth;i++)
    {
        printf("%i",data[i]);
    }
    printf("\n");
    for(i=0;i<10;i++)
    {
        data[depth]=i;
        GenerateNumbersHelper(depth+1,max_depth,data);
    }
    return 0;
}

int GenerateNumbers(int depth)
{
     int* data;
     data = malloc(sizeof(int)*depth);
     GenerateNumbersHelper(0,depth,data);
     free(data);
}

int main(void)
{
    GenerateNumbers(3);
}

关于c++ - 循环递归(生成数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5291860/

相关文章:

c# - 为非托管 (C++) 代码编写托管包装器 - 自定义类型/结构

c++ - 第一次用户的 devc++ 编译错误

c++ - [&] 在函数之前是什么意思?

generics - 递归,通用类型 - 深度类型检查

c++ - openCV SurfFeatureDetector 是否在内部不必要地提取描述符?

c - 用户输入的字符串转换为 double

c - 在公共(public)上下文中使用循环时需要简化逻辑

c - 如何监听内核中的新分支

algorithm - 在lua中生成TTT博弈树

c - 递归中递归函数的内存分配?