我是 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
# #
## ##
### ###
#### ####
##### #####
uj5u.com熱心網友回復:
遞回函式可以通過以下方式查找,如下面的演示程式所示。
#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
uj5u.com熱心網友回復:
我覺得我必須以某種方式
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");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/328827.html
上一篇:需要幫助理解基本的遞回問題
