英文:
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 <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 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论