Hello, there is a task

A ladder is a set of cubes, in which each upper layer contains cubes smaller than the previous one. It is required to write a program that calculates the number of ladders that can be built from N cubes.

Input data

The input file INPUT.TXT contains a positive integer N (1 ≀ N ≀ 100) - the number of cubes in the ladder.

Output

In the output file OUTPUT.TXT you need to display the number of ladders that can be built from N cubes.

I encountered Recursion 1 time, I understood what it was, but I don’t know how to approach this task, write in general terms how to organize everything. Thank.

PS I do not ask you to do everything for me completely, the task is not educational (any kind of lab) Here is the link to the original.

And in the task there are not steps, but the number of variations of ladders, that is, if 6 cubes are given, that is, 4 options: 6, 5 + 1.4 + 2.1 + 2 + 3.

  • I am not sure that this problem is solved by recursion. Have you tried to solve it analytically? Can addiction be derived? - Stas Litvinenko
  • To understand recursion, you must first understand recursion. - Gorets
  • I need exactly recursion (to master it), and the task is solved by recursion (judging by the title in the original task). - username76

2 answers 2

Here is the recursive function:

int stairs(int n) { int i,out; if(n<=2) return 1; // 1 ΠΈΠ»ΠΈ 2 ΠΊΡƒΠ±ΠΈΠΊΠ° ΠΌΠΎΠΆΠ½ΠΎ Ρ€Π°Π·Π»ΠΎΠΆΠΈΡ‚ΡŒ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ 1 способом out=0; for(i=0;i*2<=n;i++) // МСняСм i ΠΎΡ‚ 0 Π΄ΠΎ ΠΏΠΎΠ»ΠΎΠ²ΠΈΠ½Ρ‹ n { // ΠžΡ‚Π΄Π΅Π»ΡΠ΅ΠΌ ΠΎΡ‚ ΠΊΡƒΡ‡ΠΈ i ΠΊΡƒΠ±ΠΈΠΊΠΎΠ² ΠΈ смотрим сколькими способами ΠΈΡ… ΠΌΠΎΠΆΠ½ΠΎ Ρ€Π°Π·ΠΎΠ»ΠΆΠΈΡ‚ΡŒ: out += stairs(i); // Если ΠΎΡ‚Π΄Π΅Π»ΠΈΠ»ΠΈ Ρ€ΠΎΠ²Π½ΠΎ ΠΏΠΎΠ»ΠΎΠ²ΠΈΠ½Ρƒ, Ρ‚ΠΎ ΡƒΠ±ΠΈΡ€Π°Π΅ΠΌ ΠΈΠ· подсчСта случай, ΠΊΠΎΠ³Π΄Π° n раскладываСтся Π½Π° Π΄Π²Π° слоя ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠΉ ΡˆΠΈΡ€ΠΈΠ½Ρ‹: if(i*2==n) out--; } return out; } 

I think you will understand with input-output.

  • Thanks, it seems to work, I'll try to figure out your code now, for me it looks completely unclear. - username76
  • and you can still store all the already calculated values ​​of the stairs function, so as not to count it once again - andrybak
  1. get the number - the number of cubes
  2. call the line counting method
  3. we get the result

in the method itself

  1. took meaning
  2. minus 1 from it
  3. plyusanul to the number of steps
  4. calling ourselves recursively
  5. got the result
  • What if the main function is recursive? Or is it nonsense? - username76
  • yes, it is unlikely that you will succeed =) - Gorets
  • Immediately make a reservation, my option is wrong according to the conditions of the problem, since it does not consider ALL decomposed ladders (you can just take all the elements minus 1 - the first step, and 1 - the second) - Gorets
  • Recursive main ()? This is easy: main (int ac, char ** av, char ** ev) {if (ac> 0) {printf ("% d \ n", ac); main (- ac, av, ev); } return 0; } - avp
  • @ username76 gives me an error - Alisher Yerkimbekov