Skip to content

Instantly share code, notes, and snippets.

@unilecs
Last active August 26, 2019 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unilecs/dcfcffbc3de28d0d2683957a83cc08c2 to your computer and use it in GitHub Desktop.
Save unilecs/dcfcffbc3de28d0d2683957a83cc08c2 to your computer and use it in GitHub Desktop.
Задача Ступеньки
using System;
public class Program
{
private static long GetAllCombinations(int n)
{
long[] memoArr = new long[n + 1];
return GetCombinationCount(0, n, ref memoArr);
}
private static long GetCombinationCount(int index, int n, ref long[] memoArr)
{
if (index > n)
{
return 0;
}
if (index == n)
{
return 1;
}
if (memoArr[index] > 0)
{
return memoArr[index];
}
memoArr[index] = GetCombinationCount(index + 1, n, ref memoArr) + GetCombinationCount(index + 2, n, ref memoArr);
return memoArr[index];
}
private static long GetFibonacciNumber(long n)
{
if (n == 1)
{
return 1;
}
long f1 = 1;
long f2 = 2;
for (int i = 3; i <= n; i++)
{
long fi = f1 + f2;
f1 = f2;
f2 = fi;
}
return f2;
}
public static void Main()
{
Console.WriteLine("UniLecs");
Console.WriteLine(string.Format("Answer = {0}", GetAllCombinations(3))); // 3
Console.WriteLine(string.Format("Answer = {0}", GetFibonacciNumber(3))); // 3
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment