+ 3
Как решить задачу из 33 проекта по модулю в C#?
Не могу решить задачу, ответа не нашёл пытался сам, выводит перегрузку стека. Вот мой код: using System; using System.Collections.Generic; namespace SoloLearn { class Program { static void Main(string[] args) { int levels = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Points(levels)); } static int Points(int levels) { if (levels == 1){ return 1; } return Points(levels++); } } }
1 Resposta
+ 2
if (levels == 1){ return 1; }
This is your exit statement, if levels is equal to 1 the method is not longer calling it self.
So you need to go from for example 3 to 1.
instead of levels++, your need to decrease levels with 1.
So level-1. Do not use levels--, because this will first pass the value and after that decrease it.
Last you need to add the value of the previous method to accummulate the result.
return levels + Points(levels-1);
https://code.sololearn.com/cA15A14A13a2/?ref=app