Using a single function recursively to display a triangle with a minimum and maximum value

一笑奈何 提交于 2019-12-25 02:25:55

问题


This is a simple homework question, but I've been unable to solve it and honestly am wondering if it's even possible within the parameters that have been set.

#include <iostream>
using namespace std;
void triangle(int m, int n);
void drawline(char symbol, int len);

int main() {
    triangle(3,5);
    return 0;
}
// -----------------------------------------------------------------------------
void triangle(int m, int n) {
    // use recursion to complete this function
}
// -----------------------------------------------------------------------------
void drawline(char symbol, int len) {
    for (int i=0; i<len; i++)
        cout << symbol;
    cout << endl;
}

Once the recursion function is completed, the code above should return this.

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

I can make it up to the fifth star on line 3, but iterating backwards and stopping at three stars is throwing me for a loop. I need to have some type of value that stays at m's initial value and the same for n. I can't figure this out without changing m, and if I change m then I have lost my terminating point. I don't know if there is some strange math trick I should be doing with m and n, or if this is just something simple and I'm missing it.

If the triangle started at 0 every time, then this would be cake. It's keeping the minimum point that is throwing me for a loop. I'm not asking for the answer, but just a confirmation that this is possible without doing something hacky and a nudge in the right direction would be nice.

**I have searched all over and all I have found is triangles that either always start at 0, or they use a function for the first half of the triangle and another for the second.


回答1:


This should work.

void triangle(int m, int n)
{
   // Prevent stack overflow
   if ( m > n )
   {
      return;
   }

   // Draw m symbols.
   drawline('*', m);

   // If m and n are not equal, recurse.
   if ( m != n )
   {
      triangle(m+1,n);
      drawline('*', m);
   }
}



回答2:


Python pseudocode:

>>> def triangle(m, n):
...   if m > n: return
...   if m < n: 
...     print '*' * m
...     triangle(m + 1, n)
...     print '*' * m
...   elif m == n:
...     print '*' * m
... 
>>> triangle(3, 5)
***
****
*****
****
***
>>> 



回答3:


If I'm understanding your problem correctly, 'n' is the "height" at the center of the pyramid, and 'm' is the height at the edges. I'm assuming that 'n' must be greater than or equal to 'm', although you don't verify this.

Instead of thinking of building the pyramid from one edge to the other, think of drawing it from the outside to the middle. So when you call your recursive triangle function, you draw one edge, recurse to draw the middle, then draw the other edge. So, something like this:

triangle(m, n):
    if (m == n):
        drawline('*', m)
    else:        
        drawline('*', m)
        triangle(m+1, n)
        drawline('*', m)


来源:https://stackoverflow.com/questions/24878842/using-a-single-function-recursively-to-display-a-triangle-with-a-minimum-and-max

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!