drawPyramid() 函数的递归版本

Recursive version of drawPyramid() function

我是C新手,尝试实现了一个串行的drawPyramid函数。现在,我想为自己的实践实现此函数的递归版本drawPyramid_rec。但是,我被困了几个小时。不知道如何处理每一行中的前导空格......我觉得我必须以某种方式在第一次递归调用中存储 n 的值。或者可能无法实现 drawPyramid 的递归版本?请帮忙!

#include <stdio.h>

void drawPyramid(int n);

int main(void)
{
    int n;
    do
    {
        printf("Height: ");
        scanf("%i", &n);
    }
    while (n > 8 || n < 1);

    drawPyramid(n);

    return 0;
}

void drawPyramid(int n)
{
    for (int height = 1; height <= n; ++height)
    {
        for (int column = (n - height); column >= 1; --column)
        {
            printf(" "); // putchar(' ');
        }

        for (int column = 1; column <= height; ++column)
        {
            putchar('#'); // printf("#");
        }

        printf("  ");

        for (int column = 1; column <= height; ++column)
        {
            printf("#");
        }

        printf("\n");
    }
}

输出:

Height: 5
    #  #
   ##  ##
  ###  ###
 ####  ####
#####  #####

递归函数可以像下面的演示程序所示的方式查找。

#include <stdio.h>
#include <limits.h>

FILE * drawPyramid( unsigned int n, unsigned int m, FILE *fp )
{
    const char c = '#';

    if ( INT_MAX < n ) n = INT_MAX;

    if ( m < n )
    {
        fprintf( fp, "%*c", n - m , c );
        for ( unsigned int i = 1; i < m + 1; i++ )
        {
            fputc( c, fp );
        }

        fprintf( fp, "%*c", 3, c );
        for ( unsigned int i = 1; i < m + 1; i++ )
        {
            fputc( c, fp );
        }
        
        fputc( '\n', fp );

        drawPyramid( n, m + 1, fp );
    }

    return fp;
}

int main(void) 
{
    while ( 1 )
    {
        printf( "Enter the height of the pyramid (0 - exit): " );

        unsigned int n;

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

        putchar( '\n' );

        drawPyramid( n, 0, stdout );

        putchar( '\n' );;
    }

    return 0;
}

程序输出可能看起来像

Enter the height of the pyramid (0 - exit): 1

#  #

Enter the height of the pyramid (0 - exit): 2

 #  #
##  ##

Enter the height of the pyramid (0 - exit): 3

  #  #
 ##  ##
###  ###

Enter the height of the pyramid (0 - exit): 4

   #  #
  ##  ##
 ###  ###
####  ####

Enter the height of the pyramid (0 - exit): 5

    #  #
   ##  ##
  ###  ###
 ####  ####
#####  #####

Enter the height of the pyramid (0 - exit): 6

     #  #
    ##  ##
   ###  ###
  ####  ####
 #####  #####
######  ######

Enter the height of the pyramid (0 - exit): 7

      #  #
     ##  ##
    ###  ###
   ####  ####
  #####  #####
 ######  ######
#######  #######

Enter the height of the pyramid (0 - exit): 8

       #  #
      ##  ##
     ###  ###
    ####  ####
   #####  #####
  ######  ######
 #######  #######
########  ########

Enter the height of the pyramid (0 - exit): 9

        #  #
       ##  ##
      ###  ###
     ####  ####
    #####  #####
   ######  ######
  #######  #######
 ########  ########
#########  #########

Enter the height of the pyramid (0 - exit): 10

         #  #
        ##  ##
       ###  ###
      ####  ####
     #####  #####
    ######  ######
   #######  #######
  ########  ########
 #########  #########
##########  ##########

Enter the height of the pyramid (0 - exit): 0

I felt like somehow I have to store the value of n in the first recursive call.

是的,你必须保留n的值,也就是金字塔的高度。为此,您可以向您的函数添加一个额外的参数 drawPyramid,它永远不会改变它。

void drawPyramid_recursive(int n, int height)
{
    if (height == 0) // base case
    {
        return;
    }

    drawPyramid_recursive(n, height - 1);

    for (int column = (n - height); column >= 1; --column)
    {
        printf(" "); // putchar(' ');
    }

    for (int column = 1; column <= height; ++column)
    {
        putchar('#'); // printf("#");
    }

    printf("  ");

    for (int column = 1; column <= height; ++column)
    {
        printf("#");
    }

    printf("\n");
}