Last active
September 12, 2018 10:42
-
-
Save jonny-novikov/ff45376ed2aea200161ed934815e4c83 to your computer and use it in GitHub Desktop.
dp-lab-1
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; | |
using System.Linq; | |
using System.Collections.Generic; | |
public class DPLab1 | |
{ | |
public static Dictionary<int, int> memo = new Dictionary<int, int>(); | |
public static int[] fib = new int[100]; | |
public static int max_i = 3; | |
static DPLab1() | |
{ | |
fib[0] = fib[1] = fib[2] = 1; | |
} | |
public static int Fib_Recursive(int n) | |
{ | |
if (n <= 2) return 1; | |
return Fib_Recursive(n - 2) + Fib_Recursive(n - 1); | |
} | |
public static int Fib_Loop(int n) | |
{ | |
if (n < 3) return 1; | |
int f, f1 = 1, f2 = 1; | |
for(int i = 3; i <= n; i++) | |
{ | |
f = f1 + f2; | |
f1 = f2; | |
f2 = f; | |
} | |
return f2; | |
} | |
public static int Fib_Loop_Memo(int n) | |
{ | |
if (n < max_i) return fib[n]; | |
for (int i = max_i; i <= n; i++) | |
{ | |
fib[i] = fib[i-2] + fib[i-1]; | |
} | |
max_i = n; | |
return fib[n]; | |
} | |
public static int Fib_Memo(int n) | |
{ | |
if (memo.TryGetValue(n, out var ret)) | |
return ret; | |
ret = Fib_Recursive(n); | |
memo[n] = ret; | |
return ret; | |
} | |
/* | |
public static IEnumerable<int> GetFibNumbers() | |
{ | |
int k = 0; | |
} | |
*/ | |
// Hide ads on vk | |
// https://chrome.google.com/webstore/detail/custom-javascript-for-web/poakhlngfciodnhlhhgnaaelnpjljija | |
/* | |
var trial = document.getElementById('ads_left'); | |
if(trial){ | |
trial.style.display = 'none'; | |
} | |
*/ | |
} | |
Console.WriteLine(DPLab1.Fib_Loop_Memo(6)); |
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
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="ServiceStack.Text" version="5.2.0" targetFramework="net45" /> | |
<package id="ServiceStack.Client" version="5.2.0" targetFramework="net45" /> | |
<package id="ServiceStack.Interfaces" version="5.2.0" targetFramework="net45" /> | |
</packages> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment