Create a triangle out of stars using only recursion

前端 未结 10 847
独厮守ぢ
独厮守ぢ 2020-12-06 06:52

I need to to write a method that is called like printTriangle(5);. We need to create an iterative method and a recursive method (without ANY iteration). The out

10条回答
  •  时光取名叫无心
    2020-12-06 07:24

        void trianglePrint(int rows){
                int static currentRow = 1;
                int static currentStar = 1;
    
                // enter new line in this condition
                // (star > currentrow)  
    
                if (currentStar > currentRow ){
                    currentStar = 1;
                    currentRow++;
                    cout << endl;
                }
    
                if (currentRow > rows){
                    return; // finish
                }
    
                cout << "*";
                currentStar++;
    
                trianglePrint(rows);
            }
    

提交回复
热议问题