Created
June 8, 2018 14:46
-
-
Save hodzanassredin/71a40c0348df7cf2ddb73f08fd91abb1 to your computer and use it in GitHub Desktop.
Dijkstra task solution
This file contains hidden or 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.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Dkstra | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
for (int i = 1; i < 100; i++) | |
{ | |
Debug.Assert(F(i) == Solution(i)); | |
Console.WriteLine($"F({i})={F(i)}, Solution({i})={Solution(i)}"); | |
} | |
Console.ReadKey(); | |
} | |
static int F(int n) { | |
if (n == 1) | |
{ | |
return 1; | |
} | |
if ((n % 2) == 0) | |
{ | |
return F(n / 2); | |
} | |
return F(n / 2) + F(n / 2 + 1); | |
} | |
static int Solution(int n) | |
{ | |
var count1 = 1; | |
var count2 = 0; | |
while (n != 1) | |
{ | |
if (n % 2 != 0) count2 = count2 + count1; | |
else count1 = count1 + count2; | |
n = n / 2; | |
} | |
return count1 + count2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment