递归打印星形图案 [英] Recursively printing a star pattern

查看:60
本文介绍了递归打印星形图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的 C++ 数据结构类,我们的任务是打印这样的星星图案

For my c++ data structures class our assignment is to print a pattern of stars like this

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

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

模式中的行数由用户输入决定.所以如果用户输入 4,上面的模式就会打印出来.

with the number of lines in the pattern determined by the user input. So the pattern above would print if the user enters a 4.

我们之前有一个任务,我们必须打印相反的图案,就像这样

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

如果用户输入 5,上面的模式就会打印出来.这个模式,上面的那个,我没有问题.我使用 for 循环打印上半部分,然后再次递归调用该函数,然后使用相同的 for 循环以相反的方向打印下半部分.作为参考,这是我用于上述模式的代码:

We had a previous assignment where we had to print the opposite pattern, one like this

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

and the above pattern would print if the user enters a 5. This pattern, the one above, I had no problem with. I used a for loop to print the top half, then recursively called the function again, and then the same for loop to print the bottom half in the opposite direction. For reference, here's the code I used for the above pattern:

int main()
{
    int number;                                             
    cout << "Enter the number of lines in the grid: ";      

    cin >> number;                                          
    printStars(number);                                     
    cout << endl << "Grid Pattern Complete - End of Program.";
    return 0;
} // end of main 

void printStars(int num)                        
{
    if (num < 0) cout << endl << "Please enter a non negative number." << endl;

        else{

            if (num == 0) return;               

            else{
            for (int q = 1; q <= num; q++)      
            {cout << "*";}
            cout << endl;

            printStars(num - 1);        

            for (int q = 1; q <= num; q++)      
            {cout << "*";} 

            cout << endl;
        } 
    } 
} // end printStars

这个函数就像我想要的那样工作,所以我想我会用它作为参考来完成第二个任务.我遇到的问题是,虽然完成第一个任务很容易(打印一行 4 星,然后是一行 3,然后是一行 2 ,然后是一行 1,然后在相反的顺序),我似乎无法弄清楚如何格式化 for 循环以打印从一行 1 星开始的模式,然后是一行 2,然后是一行 3,依此类推,直到它被递归调用并以相反的顺序再次打印.

作为参考,这是我(到目前为止)为第二个作业编写的代码:

This function works like how I want it, so I figured I would use it as a reference to complete the second assignment. The problem I'm having is that, while it was easy enough to complete the first assignment (printing a line of 4 stars, then a line of 3, then a line of 2 , then a line of 1, then all that again in reverse order), I can't seem to figure out how to format the for loops to print the pattern starting with a line of 1 star, then a line of 2, then a line of 3, and so on, until its called recursively and printed again in reverse order.

For reference, this is the code I have (so far) for the second assignment:

int main()
{
    int number;                                             
    cout << "Enter the number of lines in the grid: ";      
    cin >> number;
    printStars(number, 0);                                 

    cout << endl << "Grid Pattern Complete - End of Program.";

    return 0;
}

void printStars(int num, int num2)
{
  if (num2 <= num)
  {

      for (int e = num; e > num2; e--)
      {
          cout << "*";
      }

      cout << endl;

      printStars(num - 1, num2);

  }
}

这唯一打印的是图案的后半部分;

(如果用户输入 5)

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

The only thing this prints is the second half of the pattern;

(If the user enters a 5)

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

即使要完成这项工作,我也必须在最后递归调用该函数,这是乱序的.

我想我只是对这种递归应该如何工作感到困惑,但我已经玩了几个小时了,我似乎无法重新格式化或重新排列或重组它,以便它像我需要的那样打印它到.有人可以给我一些指导吗?也许只是写一些伪代码来帮助我.这是给学校的,所以我需要能够理解它,但我现在真的很迷茫.

And even to make this work, I have to recursively call the function at the end, which is out of order.

I guess I'm just confused on how this recursion is supposed to work but I've been playing with it for hours and I can't seem to reformat it or rearrange it or restructure it so that it prints like I need it to. Can someone give me some guidance? Just maybe write some pseudo code to help me out. This is for school so I need to be able to understand it but I'm really lost right now.

推荐答案

试试这个.它是您代码的最小修改版本.将上限传递给所有递归,并且递归函数调用以从 1 开始的值执行(第一行仅 1 个开始):

Try this. It is minimally modified version of your code. The upper limit is passed to all recursions and the recursive function calls are performed with the values starting with 1 (only 1 start in the first line):

void printStars(int num, int limit)                         
{
        if (num >limit) return;              
        else{
        for (int q = 1; q <= num; q++)      
        {cout << "*";}
        cout << endl;

        printStars(num +1, limit);        

        for (int q = 1; q <= num; q++)      
        {cout << "*";} 

        cout << endl;
    } 

}

int main()
{        
    int number=5;  
    cin>>number;                                
    printStars(1, number);                                     

    return 0;
} // end of main 

我测试过,结果是正确的.链接是:

I tested it and the result is correct. The link is:

http://ideone.com/ez6pZ5

ideone 结果:

Success time: 0 memory: 3144 signal:0

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

这篇关于递归打印星形图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆