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); } 

Closed due to the fact that the essence of the question is not clear to the participants of Harry , Kromster , freim , LFC , aleksandr barakin on March 5 at 11:13 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • You have not really formulated the task. What kind of "solution" are we talking about? - AnT

2 answers 2

Rounded down a positive solution to a quadratic equation:

 (k + 1) * k / 2 = n k = floor((-1 + sqrt(1 + 8 * n)) / 2) 

(Hello from little Gauss.)

  • Can the solution from the question be transferred to the answer? - michael_best 7:38 pm
  • @michael_best I don't like the solution from the question. I am against cycles and recursions where the answer is simply considered. - Igor
  • This is a laboratory and in front of me was the task to solve recursively and simply find the maximum height of the ladder, and not all its variations. - Vladimir Syrykh
  • @Vladimir Syrykh Then exactly state what you have done “for future generations” - with all the restrictions. - Harry
  • @ Vladimir Syrykh "When you’re a tool, it’s like a nail.” - Igor

The tasks “looking for the maximum height of a ladder” and “the number of ladders from N cubes” are different.

I will answer the second, because it says “needs to be found”, and more or less clear wording is given:

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.

This is the number of partitions of N into various components (1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 15, 18, 22..)

 import functools @functools.lru_cache(maxsize=None) #простейшая мемоизация def growparts(n, last): if n == 0: return 1 result = 0 for i in range(last + 1, n + 1): result += growparts(n - i, i) return result for k in range(15): print(k, growparts(k, 0)) 
  • And this is what programming language? - Vladimir Syrykh
  • This is Python, but except for optional functools, everything else is not specific ( int growparts(int n, int last) { if (n==0) return 1 ...) - MBo