Advertisement
vencinachev

Recursions3

Sep 7th, 2021
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Recursion
  6. {
  7.     class Program
  8.     {
  9.  
  10.         static int Fact(int n)
  11.         {
  12.             if (n == 0)
  13.             {
  14.                 return 1;
  15.             }
  16.             return Fact(n - 1) * n;
  17.         }
  18.  
  19.         static void PrintNumbers(int n)
  20.         {
  21.             if (n == 0)
  22.             {
  23.                 return;
  24.             }
  25.             Console.Write($"{n} ");
  26.             PrintNumbers(n - 1);
  27.             Console.Write($"{n} ");
  28.         }
  29.  
  30.         static void DrawTriangle(int n)
  31.         {
  32.             if (n == 0)
  33.             {
  34.                 return;
  35.             }
  36.             Console.WriteLine(new string('*', n));
  37.             DrawTriangle(n - 1);
  38.             Console.WriteLine(new string('*', n));
  39.         }
  40.  
  41.         static double Power(double a, int n)
  42.         {
  43.             if (n == 0)
  44.             {
  45.                 return 1;
  46.             }
  47.             return a * Power(a, n - 1);
  48.         }
  49.  
  50.         static double FastPower(double a, int n)
  51.         {
  52.             if (n == 0)
  53.             {
  54.                 return 1;
  55.             }
  56.             else if (n % 2 == 0)
  57.             {
  58.                 double x = FastPower(a, n / 2);
  59.                 return x * x;
  60.             }
  61.             else
  62.             {
  63.                 return a * FastPower(a, n - 1);
  64.             }
  65.         }
  66.  
  67.        
  68.         static int SumArr(int[] arr, int start)
  69.         {
  70.             if (start == arr.Length)
  71.             {
  72.                 return 0;
  73.             }
  74.             return arr[start] + SumArr(arr, start + 1);
  75.         }
  76.  
  77.         static int ProductArr(int[] arr, int start)
  78.         {
  79.             if (start == arr.Length)
  80.             {
  81.                 return 1;
  82.             }
  83.             return arr[start] * ProductArr(arr, start + 1);
  84.         }
  85.  
  86.         static int SumD(int n)
  87.         {
  88.             if (n < 10)
  89.             {
  90.                 return n;
  91.             }
  92.             return n % 10 + SumD(n / 10);
  93.         }
  94.  
  95.         static int DigitalRoot(int n)
  96.         {
  97.             if (n < 10)
  98.             {
  99.                 return n;
  100.             }
  101.             return DigitalRoot(SumD(n));
  102.         }
  103.  
  104.         static int gcd(int a, int b)
  105.         {
  106.             if (b == 0)
  107.             {
  108.                 return a;
  109.             }
  110.             return gcd(b, a % b);
  111.         }
  112.  
  113.  
  114.         static void HanoiTowers(int disks, char src, char mid, char dest)
  115.         {
  116.             if (disks == 1)
  117.             {
  118.                 Console.WriteLine($"{src} -> {dest}");
  119.                 return;
  120.             }
  121.             HanoiTowers(disks - 1, src, dest, mid);
  122.             HanoiTowers(1, src, mid, dest);
  123.             HanoiTowers(disks - 1, mid, src, dest);
  124.         }
  125.  
  126.  
  127.         static int LinearSeach(int[] arr, int x)
  128.         {
  129.             for (int i = 0; i < arr.Length; i++)
  130.             {
  131.                 if (arr[i] == x)
  132.                 {
  133.                     return i;
  134.                 }
  135.             }
  136.             return -1;
  137.         }
  138.  
  139.         static int BinarySearch(int[] arr, int left, int right, int x)
  140.         {
  141.             if (right >= left)
  142.             {
  143.                 int mid = left + (right - left) / 2;
  144.                 if (arr[mid] == x)
  145.                 {
  146.                     return mid;
  147.                 }
  148.                 else if (x < arr[mid])
  149.                 {
  150.                     Console.WriteLine("DOWN");
  151.                     return BinarySearch(arr, left, mid - 1, x);
  152.                 }
  153.                 else
  154.                 {
  155.                     Console.WriteLine("UP");
  156.                     return BinarySearch(arr, mid + 1, right, x);
  157.                 }
  158.             }
  159.             return -1;
  160.         }
  161.        
  162.         static int BinarySearch(int[] arr, int x)
  163.         {
  164.             return BinarySearch(arr, 0, arr.Length, x);
  165.         }
  166.  
  167.  
  168.         static int[] SortedMerge(int[] a, int[] b)
  169.         {
  170.             int[] c = new int[a.Length + b.Length];
  171.             int ia = 0, ib = 0, ic = 0;
  172.             while (ic < c.Length)
  173.             {
  174.                 if (ia < a.Length && ib < b.Length)
  175.                 {
  176.                     if (a[ia] < b[ib])
  177.                     {
  178.                         c[ic] = a[ia];
  179.                         ia++;
  180.                     }
  181.                     else
  182.                     {
  183.                         c[ic] = b[ib];
  184.                         ib++;
  185.                     }
  186.                 }
  187.                 else if (ia < a.Length)
  188.                 {
  189.                     c[ic] = a[ia];
  190.                     ia++;
  191.                 }
  192.                 else
  193.                 {
  194.                     c[ic] = b[ib];
  195.                     ib++;
  196.                 }
  197.                 ic++;
  198.             }
  199.             return c;
  200.         }
  201.  
  202.         static int[] MergeSort(int[] arr)
  203.         {
  204.             int[] left, right;
  205.  
  206.             int[] result = new int[arr.Length];
  207.  
  208.             if (arr.Length <= 1)
  209.             {
  210.                 return arr;
  211.             }
  212.             int mid = arr.Length / 2;
  213.  
  214.             left = new int[mid];
  215.  
  216.             if (arr.Length % 2 == 0)
  217.             {
  218.                 right = new int[mid];
  219.             }
  220.             else
  221.             {
  222.                 right = new int[mid + 1];
  223.             }
  224.  
  225.             for (int i = 0; i < mid; i++)
  226.             {
  227.                 left[i] = arr[i];
  228.             }
  229.  
  230.             for (int i = mid, j = 0; i < arr.Length; i++, j++)
  231.             {
  232.                 right[j] = arr[i];
  233.             }
  234.  
  235.             left = MergeSort(left);
  236.             right = MergeSort(right);
  237.  
  238.             result = SortedMerge(left, right);
  239.  
  240.             return result;
  241.         }
  242.  
  243.         static void Main(string[] args)
  244.         {
  245.             int[] a = { 1, 2434, 54, -34, 43, 544, 1054, 204, 78};
  246.             int[] result = MergeSort(a);
  247.             for (int i = 0; i < result.Length; i++)
  248.             {
  249.                 Console.Write("{0} ", result[i]);
  250.             }
  251.         }
  252.     }
  253. }
  254.  
  255.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement