Advertisement
vencinachev

Combinatorics

Sep 7th, 2019
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. class Program
  2.     {
  3.         static int[] elements;
  4.         static bool[] used;
  5.         static int[] permutate;
  6.  
  7.         static void Gen (int index)
  8.         {
  9.             if (index >= elements.Length)
  10.             {
  11.                 Console.WriteLine(string.Join(',', permutate));
  12.             }
  13.             else
  14.             {
  15.                 for (int i = 0; i < elements.Length; i++)
  16.                 {
  17.                     if (!used[i])
  18.                     {
  19.                         used[i] = true;
  20.                         permutate[index] = elements[i];
  21.                         Gen(index + 1);
  22.                         used[i] = false;
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.  
  28.         static int[] variations;
  29.         static void GenVar(int index, int k)
  30.         {
  31.             if (index >= k)
  32.             {
  33.                 Console.WriteLine(string.Join(',', variations));
  34.             }
  35.             else
  36.             {
  37.                 for (int i = 0; i < elements.Length; i++)
  38.                 {
  39.                     if (!used[i])
  40.                     {
  41.                         used[i] = true;
  42.                         variations[index] = elements[i];
  43.                         GenVar(index + 1, k);
  44.                         used[i] = false;
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.  
  50.         static void PrintPascalTriangle(int height)
  51.         {
  52.             long[][] triangle = new long[height + 1][];
  53.             for (int row = 0; row < height; row++)
  54.             {
  55.                 triangle[row] = new long[row + 1];
  56.             }
  57.             triangle[0][0] = 1;
  58.             for (int row = 0; row < height - 1; row ++)
  59.             {
  60.                 for (int col = 0; col <= row; col++)
  61.                 {
  62.                     triangle[row + 1][col] += triangle[row][col];
  63.                     triangle[row + 1][col + 1] += triangle[row][col];
  64.                 }
  65.             }
  66.  
  67.             for (int row = 0; row < height; row++)
  68.             {
  69.                 Console.Write("".PadLeft((height - row) * 2));
  70.                 for (int col = 0; col <= row; col++)
  71.                 {
  72.                     Console.Write("{0,3} ", triangle[row][col]);
  73.                 }
  74.                 Console.WriteLine();
  75.             }
  76.         }
  77.        
  78.         static void Main(string[] args)
  79.         {
  80.             elements = new int[]{ 1, 2, 3, 4, 5 };
  81.             used = new bool[5];
  82.             permutate = new int[5];
  83.             variations = new int[2];
  84.             GenVar(0, 2);
  85.             PrintPascalTriangle(12);
  86.         }
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement