Advertisement
Lyubohd

Вариант-3-Задача-3

Feb 2nd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ExamPractice
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int n = int.Parse(Console.ReadLine());
  10.  
  11.             int ni = FindElementN(n);
  12.             Console.WriteLine("N-тия член на редицата е " + ni);
  13.             int recursionNi = recursiveElement(n);
  14.             Console.WriteLine("N-тия член на редицата намерен с рекурсия е " + recursionNi);
  15.         }
  16.  
  17.         private static int recursiveElement(int n)
  18.         {
  19.             if (n == 1)
  20.             {
  21.                 return 1;
  22.             }
  23.             if (n == 2)
  24.             {
  25.                 return 3;
  26.             }
  27.             if (n == 3)
  28.             {
  29.                 return 4;
  30.             }
  31.             int ni = recursiveElement(n - 1) - recursiveElement(n - 2) + 2 * recursiveElement(n - 3);
  32.             return ni;
  33.         }
  34.  
  35.         private static int FindElementN(int n)
  36.         {
  37.             int n1 = 1;
  38.             int n2 = 3;
  39.             int n3 = 4;
  40.  
  41.             int ni = 0;
  42.  
  43.             for (int i = 4; i <= n; i++)
  44.             {
  45.                 ni = n3 - n2 + 2 * n1;
  46.                 n1 = n2;
  47.                 n2 = n3;
  48.                 n3 = ni;
  49.             }
  50.  
  51.             return ni;
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement