-
-
Save unilecs/dcfcffbc3de28d0d2683957a83cc08c2 to your computer and use it in GitHub Desktop.
Задача Ступеньки
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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