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

24th Feb 2021, 10:38 AM
bench_
bench_ - avatar
1 Réponse
+ 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
24th Feb 2021, 7:32 PM
sneeze
sneeze - avatar