This topic is not a question, but a working solution to this problem in a recursive form and it looks for the maximum height of the ladder, nothing more. As noted in the comments, you can solve this problem through an arithmetic progression. A ladder is a set of cubes, in which each top layer contains cubes smaller than the previous one. It is required to write a program that calculates the number of ladders from N cubes. I searched the Internet for a long time for an adequately working version and nevertheless decided to do it myself and help someone with a similar problem. I publish the decision below. For those who want to understand. First, the very first layer is only one cube, in subsequent iterations a layer of length greater than 1 is created than the previous one. And so on until the result is achieved.
int layersCount = 1; int Stairs(int n, int previousLayer) { int thisLayer=0; while (thisLayer - previousLayer !=1 && n - (thisLayer + previousLayer)>0) thisLayer++; if (thisLayer - previousLayer == 1) layersCount += 1; if (n - thisLayer-previousLayer > 0) return Stairs(n - previousLayer, thisLayer); else return layersCount; } int main() { int n; std::cin >> n; std::cout << Stairs(n,1); }