Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;星的递归输出_C++_Recursion - Fatal编程技术网

C++ c++;星的递归输出

C++ c++;星的递归输出,c++,recursion,C++,Recursion,我正在努力做这个练习 编写递归函数以生成星形图案,如下所示: * ** *** **** **** *** ** * 这看起来很简单,但给了我很多问题。我只能输出这个 * ** *** **** 使用以下代码 #include <iostream> using namespace std; void outputStars(int); int main() { outputStars(5); return 0; } void outputStars(int

我正在努力做这个练习

编写递归函数以生成星形图案,如下所示:

*
**
***
****
****
***
**
*
这看起来很简单,但给了我很多问题。我只能输出这个

*
**
***
****
使用以下代码

#include <iostream>

using namespace std;

void outputStars(int);

int main()
{
    outputStars(5);
    return 0;
}

void outputStars(int num)
{
    if (num == 1)
    {
        return;
    }

    outputStars(--num);

    for (int i = 0; i < num  ; i++)
    {
        cout << "*";
    }

    cout << endl;   
}
#包括
使用名称空间std;
空输出星(int);
int main()
{
输出星(5);
返回0;
}
无效输出星号(整数)
{
如果(num==1)
{
回来
}
输出星(--num);
for(int i=0;icout您只有在通话结束后才打印星号。也可以在通话前打印星号

我已将其修改为接受2个参数。
I
指定调用中打印
*
的次数

void outputStars(int num,int i)
{
    if (i == num)
    {
        return;
    }
    for (int j = 0 ;j < i; j++)
    {
        cout << "*";
    }
    cout << endl;

    outputStars(num,i+1);
    for (int j = 0; j < i; j++)
    {
        cout << "*";
    }
    cout << endl;
#包括
无效记录(int x);
无效打印(整数倍,整数x);
int main(int argc,char*argv[])
{
娱乐中心(5);
返回0;
}
无效记录乐趣(int x)
{
静态整数倍=x;

如果(x)你的问题是什么?你有没有考虑过在通往锚点的路径上做点什么,在锚点向上的路径上做点什么?我认为你应该从获取
outputStars开始(1)
。变异,如递减变量,通常不适合递归。这里的可能重复很可能是你作业的答案……你应该自己尝试另一种方法,这就是作业的重点
outputStars(5,1);
#include <stdio.h>

void rec_fun(int x);
void print_stars(int times, int x);

int main(int argc, char *argv[])
{
    rec_fun(5);

    return 0;
}

void rec_fun(int x)
{
    static int times = x;

    if (x <= 0)
        return;

    print_stars(times, x);
    rec_fun(x - 1);
    if (x != 1)
        print_stars(times, x);

}

void print_stars(int times, int x)
{
    int i;

    for (i = 0; i < times - x + 1; i++)
        printf("*");
    printf("\n");
}