【问题标题】:Recursive version of drawPyramid() functiondrawPyramid() 函数的递归版本
【发布时间】:2021-11-29 12:15:39
【问题描述】:

我是 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
    #  #
   ##  ##
  ###  ###
 ####  ####
#####  #####

【问题讨论】:

  • 请展示您对递归解决方案的尝试
  • “我觉得我必须在第一次递归调用中存储 n 的值。” -> 是的,或者函数应该以其他方式知道(最大)高度,例如使用static 或全局变量。这个额外的变量是必要的,因为在每一行函数必须知道两个独立的值:空格数和# 字符数。

标签: c recursion function-definition


【解决方案1】:

我觉得我必须在第一次递归调用中存储n 的值。

是的,您必须保留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");
}

【讨论】:

    【解决方案2】:

    递归函数可以如下面的演示程序所示。

    #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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      • 2013-03-08
      • 2012-06-07
      • 2012-01-26
      相关资源
      最近更新 更多