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:
- Display character
- Number of characters displayed
- Number of empty characters
or so you have:
- Displayed characters
- Number of characters displayed
- 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.