I solve this problem. It is simple, but on it I decided to learn how to write recursion and draw up flowcharts of recursive calls. It is necessary to derive the ladder from the characters # . Is it possible in my implementation to get rid of the third parameter of the solve function to make it more beautiful?

 #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <string> using namespace std; void solve(const string& s, int len, int n) { if(s.size() > 0) { solve(s.substr(0, len - 1), len - 1, n); cout << string(n - len, ' ') << s << endl; } } int main(){ int n; cin >> n; solve(string(n, '#'), n, n); return 0; } 

    4 answers 4

    Let's consider the string output as a closed environment. The closed environment for proper operation is described by the variables it accepts. In this case, it is described by the following minimum knowledge:

    1. Display character
    2. Number of characters displayed
    3. Number of empty characters

    or so you have:

    1. Displayed characters
    2. Number of characters displayed
    3. The length of the string from which empty characters are calculated.

    You can get rid of the parameter only if you can come up with a system that can accept fewer parameters for correct operation or a system that can output some parameters through others . In this case, there is a variant with the output of one parameter, through another: the length of the string from the string itself. Unfortunately, this is not the third parameter, but the second one. Since you are still taking size.

     #include <iostream> #include <string> using namespace std; void solve(const string& s, int n) { int len = s.size(); if(len > 0) { solve(s.substr(0, len - 1), n); cout << string(n - len, ' ') << s << endl; } } int main(){ int n; cin >> n; solve(string(n, '#'), n); return 0; } 

    Well or for good, the solution requires only the number of characters left and right, if you already know the displayed characters initially. They can be derived from only the total length amount + indentation of recursion, as @ gil9red did in its response.

      Yes Easy :)

       void solve(pair<int,int> n) { if (n.first == 0) return; solve(make_pair(n.first-1,n.second)); for(int i = 0; i < n.second; ++i) cout << ((i < n.second-n.first) ? ' ' : '#'); cout << endl; } int main(){ int n; cin >> n; solve(make_pair(n,n)); return 0; } 

      Just once you need a ladder aligned to the right - you need to somehow pass this width to all the challenges. It is possible by parameter, it is possible global variable ... But each call should know this width.

      By the way, all this can be hidden in one line. Just pass a string of n characters # , and then recursively pass a string replacing the first # space, well, and when the whole string is from one space, then the recursion is the end ...

        For example:

         void solve2(int n, int max) { if (n > 0) { cout << string(n - 1, ' ') << string(max - n + 1, '#') << endl; solve2(n - 1, max); } } void solve2(int n) { solve2(n, n); } int main() { int n = 6; // cin >> n; solve(string(n, '#'), n, n); cout << "-----------\n"; solve2(n); return 0; } 

        Result:

          # ## ### #### ##### ###### ----------- # ## ### #### ##### ###### 
        • And without a wrapper can be? - typemoon pm
        • What wrapper? Tried to guess and updated code - gil9red
        • solve2 over solve. Oh, did not notice that you have not the way I thought. In the main function, the name solve instead of solve2, and I thought that you made a wrapper for my function. - typemoon
        • Bad psychic because of me) - gil9red

        Result in 0 parameters) If the goal is exactly that, then here is the solution.

         #include <iostream> #include <string.h> using namespace std; char *z; void Rec(){ char *q; if ( q = strrchr(z,' ') ){ q[0] = '#'; cout << z << endl; Rec(); } } int main() { z = new char[7]; for (int i=0;i<6;i++) z[i] = ' '; z[6] = 0; Rec(); return 0; }