Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. using System;
  2.  
  3. namespace OkePreparing
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Console.WriteLine("Hello World!");
  10. //Console.Write(typeof(string).Assembly.ImageRuntimeVersion);
  11.  
  12. var fibbMethod = GetFibbonaciMethod(FibboObject.Normal);
  13.  
  14. var result = fibbMethod.CountFibbonaci(10);
  15.  
  16. var recursiveMethod = GetFibbonaciMethod(FibboObject.Recursive);
  17.  
  18. var recursiveResult = recursiveMethod.CountFibbonaci(10);
  19.  
  20. var tailResusive = GetFibbonaciMethod(FibboObject.TailRecursive);
  21.  
  22. var tailRecursiveResult = tailResusive.CountFibbonaci(10);
  23.  
  24. Console.WriteLine($"{result} for 10 - normal");
  25. Console.WriteLine($"{recursiveResult} for 10 - recursive");
  26. Console.WriteLine($"{tailRecursiveResult} for 10 - tail recursive");
  27.  
  28. Console.Read();
  29.  
  30. }
  31.  
  32. public static IFibbonaciFactory GetFibbonaciMethod(FibboObject obj)
  33. {
  34. IFibbonaciFactory fibb = null;
  35.  
  36. switch (obj)
  37. {
  38. case FibboObject.Normal:
  39. fibb = new NormalFibbonaci();
  40. break;
  41. case FibboObject.Recursive:
  42. fibb = new RecursiveFibbonaci();
  43. break;
  44. case FibboObject.TailRecursive:
  45. fibb = new TailRecursiveFibbonaci();
  46. break;
  47. }
  48.  
  49. return fibb;
  50. }
  51. }
  52.  
  53. public interface IFibbonaciFactory
  54. {
  55. int CountFibbonaci(int n);
  56. }
  57.  
  58. public class NormalFibbonaci : IFibbonaciFactory
  59. {
  60. public int CountFibbonaci(int n)
  61. {
  62. if (n <= 1)
  63. {
  64. return 1;
  65. }
  66. var fibbArray = new int[n];
  67. fibbArray[0] = 1;
  68. fibbArray[1] = 1;
  69.  
  70.  
  71. for (int i = 2; i < n; i++)
  72. {
  73. fibbArray[i] = fibbArray[i - 1] + fibbArray[i - 2];
  74. }
  75.  
  76. return fibbArray[n - 1];
  77. }
  78. }
  79.  
  80. public class RecursiveFibbonaci : IFibbonaciFactory
  81. {
  82. public int CountFibbonaci(int n)
  83. {
  84. if (n == 0)
  85. {
  86. return 0;
  87. }
  88.  
  89. if (n == 1)
  90. {
  91. return 1;
  92. }
  93.  
  94. return CountFibbonaci(n - 1) + CountFibbonaci(n - 2);
  95. }
  96. }
  97.  
  98. public class TailRecursiveFibbonaci : IFibbonaciFactory
  99. {
  100. public int CountFibbonaci(int n)
  101. {
  102. return CountTailRecursiveFibb(n, 0, 1);
  103. }
  104.  
  105. private int CountTailRecursiveFibb(int n, int e1, int e2)
  106. {
  107. if (n == 0)
  108. {
  109. return e1;
  110. }
  111.  
  112. if (n == 1)
  113. {
  114. return e2;
  115. }
  116.  
  117. return CountTailRecursiveFibb(n - 1, e2, e1 + e2);
  118. }
  119. }
  120.  
  121. public enum FibboObject
  122. {
  123. Normal,
  124. Recursive,
  125. TailRecursive
  126. }
  127.  
  128. //public interface INameFactory
  129. //{
  130. // void ShowName()
  131. // {
  132. // Console.WriteLine("name");
  133. // }
  134. //}
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement