如何使用递归而不使用循环在C中打印星形金字塔图案?

huangapple go评论82阅读模式
英文:

How do I print a star pyramid pattern using recursion without any loop in C?

问题

以下是翻译好的部分:

这是输出结果:

*
* * * 
* * * * * 
* * * * * * *

我不希望在我的代码中出现任何循环,比如for循环、do/while循环或while循环。

英文:

I do this but it's using a for loop. I don't want it:

#include <stdio.h>

void printPattern(int n);

int main()
{
    int n = 4;  
    printPattern(n);
    return 0;
}

void printPattern(int n)
{
    if (n == 1)
    {
       printf("*\n");
       return;
    }

    printPattern(n - 1);

    for (int i = 0; i < (2 * n - 1); i++)
    {
        printf("* ");
    }
    printf("\n");
}

This is the output:

*
* * * 
* * * * * 
* * * * * * *

I don't want any loop in my code like a for loop, a do/while loop or a while loop.

答案1

得分: 1

你可以通过调用这个函数来替代这部分代码:

void print_asterisk(int amount)
{
  if (amount == 0)
    return;
  else
  {
    print("**");
    print_asterisk(amount - 1);
  }
}

这样,你就用递归调用替代了你的for循环。

英文:

You can replace this:

for(int i=0;i< (2*n-1);i++)
{
    printf("* ");
}

by calling this function (not tested):

void print_asterisk(int amount)
{
  if (amount == 0)
  return;
  else
  {
    print ("**");
    print_asterisk(amount - 1);
  }
}

Like this, you have replaced your for-loop by a recursive call.

答案2

得分: -3

这个任务对像你和我一样的初学者来说并不容易。

但我们初学者应该互相帮助。

我可以建议以下解决方案,如下演示程序所示。

#include <stdio.h>

void printPattern(int n)
{
    if (n < 0)
    {
        printf("* ");
        printPattern(n + 1);
    }
    else if (n == 0)
    {
        putchar('\n');
    }
    else
    {
        if (n > 1)
        {
            printPattern(n - 1);
        }
        printPattern(-2 * n + 1);
    }
}

int main(void)
{
    for (int n = 0; n < 10; ++n)
    {
        printPattern(n + 1);
        putchar('\n');
    }
}

程序的输出是:

*

*
* * *

*
* * *
* * * * *

*
* * *
* * * * *
* * * * * * *

*
* * *
* * * * *
* * * * * * * *
* * * * * * * * * * *

*
* * *
* * * * *
* * * * * * * *
* * * * * * * * * * * *

*
* * *
* * * * *
* * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *

*
* * *
* * * * *
* * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *

*
* * *
* * * * *
* * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *

*
* * *
* * * * *
* * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *

另一种方法是编写两个递归函数。第一个函数将调用自身来确定金字塔的行数,另一个函数将被递归调用以输出一行。然而,在这种情况下,您将有两个函数而不是一个递归函数。

英文:

The assignment is not easy for beginners like you and me.

But we, beginners, should help each other.

I can suggest the following solution shown in the demonstration program below.

#include &lt;stdio.h&gt;

void printPattern( int n )
{
	if (n &lt; 0)
	{
		printf( &quot;* &quot; );
		printPattern( n + 1 );
	}
	else if (n == 0)
	{
		putchar( &#39;\n&#39; );
	}
	else
	{
		if (n &gt; 1)
		{
			printPattern( n - 1 );
		}
		printPattern( -2 * n + 1 );
	}
}

int main( void )
{
	for (int n = 0; n &lt; 10; ++n)
	{
		printPattern( n + 1 );
		putchar( &#39;\n&#39; );
	}
}

The program output is

*

*
* * *

*
* * *
* * * * *

*
* * *
* * * * *
* * * * * * *

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * *

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *

Another approach is to write two recursive functions. The first one will call itself for the number of rows in the pyramid and other will be recursively called to output a row. However in this case you will have two functions instead of one recursive function.

huangapple
  • 本文由 发表于 2023年6月22日 19:57:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定