【问题标题】:Print a console "picture" using recursion使用递归打印控制台“图片”
【发布时间】:2018-02-15 13:16:36
【问题描述】:

我在打印以下图片时遇到了一些问题。

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 次,然后使用参数nk + 1 调用pyram。仅当k &lt;= 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);
    }
}

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

【问题讨论】:

  • 也许是时候给你learn how to debug your programs了?
  • 这个错误的行实际上是不需要的,因为current 永远不会大于n,你仍然应该删除它。对于工作代码,您可以在codereview.stackexchange.com 获得反馈,但在您确保它确实工作之前;)
  • @drescherjm,是的,这是一个错字。我已经更正了,谢谢。
  • 如果此代码有效(您说它“似乎有效”),那么您可以考虑将其发布到CodeReview 以供审核。请注意,在 Stack Exchange 网络中不赞成交叉发布,因此如果您这样做,则需要删除此问题。
  • 我喜欢这个关于递归的解释:“递归 - 参见递归”

标签: c function loops for-loop recursion


【解决方案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' );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多