c - 使用递归打印控制台 "picture"

标签 c function loops for-loop recursion

打印下面的图片时遇到一些问题。

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1       (16 times)
    2 2 2 2 2 2 2 2 2 2 2 2           (12 times)
        3 3 3 3 3 3 3 3                (8 times)
            4 4 4 4                    (4 times)
        3 3 3 3 3 3 3 3                (8 times)
    2 2 2 2 2 2 2 2 2 2 2 2           (12 times)
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1       (16 times)

实现迭代算法对我来说很容易,但我必须使用递归。我编写了以下代码(C++),似乎可以完成这项工作。

void print(int n, int current)
{
    int offset = (n / 2) * (current - 1);
    int i;

    for (i = 0; i < offset; i++)
        printf("  ");
    for (i = 1; i <= (n - current + 1) * n; i++)
        printf("%i ", current);
    printf("\n");
}

void picture(int n, int current)
{
    if (current < n) {  
        print(n, current);
        picture(n, current + 1);
        print(n, current);
    }
    else
        if (current == n)
            print(n, current);
}


int main()
{
int n;
    input: printf("Enter n --> ");
    scanf_s("%i", &n);
    if ((n < 1) || (n > 9) || (n % 2 == 1)) {
        printf("ERROR: n must be an even decimal digit!\n");
        goto input;
    }

    picture(n, 1);
    return 0;
}

我想知道这里是否有更简单的方法来编写递归函数。

更新:我尝试在打印“金字塔”的更简单问题中识别递归:

1
2 2
3 3 3 
4 4 4 4
5 5 5 5 5

函数pyram接收两个参数:最大数量n (在我们的例子中为 5)和当前号码 kk打印 k次,然后pyram使用参数 n 调用和k + 1 。仅当 k <= n 时才会发生这种情况.

void pyram(int n, int k)
{
    if (k <= n) {
        for (int i = 1; i <= k; i++)
            printf("%i ", k);
        printf("\n");
        pyram(n, k + 1);
    }
}

我已经以类似的方式编写了原始问题的解决方案。

最佳答案

您可以在递归函数中使用静态变量。在这种情况下,函数声明看起来会更简单,并且不需要辅助函数。

例如

#include <stdio.h>

void display_pattern( unsigned int n )
{
    const unsigned int FACTOR = 4;
    static unsigned int value = 1;
    static int indent = 1;

    if ( n )
    {
        printf( "%*u", indent, value );
        for ( unsigned int i = 1; i < FACTOR * n; i++ ) printf( " %u", value );
        putchar( '\n' );

        indent += FACTOR;
        ++value;

        display_pattern( --n );

        indent -= FACTOR;
        --value;
    }

    if ( n++ )
    {
        printf( "%*u", indent, value );
        for ( unsigned int i = 1; i < FACTOR * n; i++ ) printf( " %u", value );
        putchar( '\n' );
    }       
}

int main(void) 
{
    const unsigned int N = 10;

    while ( 1 )
    {
        printf( "Enter a non-negative number less than %u (0 - exit): ", N );

        unsigned int n;

        if ( scanf( "%u", &n ) != 1 || n == 0 ) break;

        if ( !( n < N ) ) n = N - 1;

        putchar( '\n' );
        display_pattern( n );
        putchar( '\n' );
    }

    return 0;
}

程序输出如下所示

Enter a non-negative number less than 10 (0 - exit): 10

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
        3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
            4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
                5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
                    6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
                        7 7 7 7 7 7 7 7 7 7 7 7
                            8 8 8 8 8 8 8 8
                                9 9 9 9
                            8 8 8 8 8 8 8 8
                        7 7 7 7 7 7 7 7 7 7 7 7
                    6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
                5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
            4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
        3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
    2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Enter a non-negative number less than 10 (0 - exit): 4

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    2 2 2 2 2 2 2 2 2 2 2 2
        3 3 3 3 3 3 3 3
            4 4 4 4
        3 3 3 3 3 3 3 3
    2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Enter a non-negative number less than 10 (0 - exit): 0

至于函数pyram,它可以看起来像

void display_triangle( unsigned int n )
{
    if ( n )
    {
        display_triangle( n - 1 );
        for ( unsigned int i = 0; i < n; i++ ) printf( "%u ", n );
        putchar( '\n' );
    }
}

关于c - 使用递归打印控制台 "picture",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48808219/

相关文章:

javascript - 3维数组到对象数组

c - 打印二维数组内容的不同方式

R:如何将循环先前子元素的函数应用于列表

C 程序将整数读入数组然后打印它

excel - 试图让 vba 嵌套 for 循环以用于电子邮件和 pdf 导出

C++如何创建一个接受字符串并返回函数的映射

c - 从服务器接收 UDP 消息时出现问题

c - 从 C 获取 Lua 字符串

c - 打印指针值

c++将二维数组传递给函数