Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static int[] elements;
- static bool[] used;
- static int[] permutate;
- static void Gen (int index)
- {
- if (index >= elements.Length)
- {
- Console.WriteLine(string.Join(',', permutate));
- }
- else
- {
- for (int i = 0; i < elements.Length; i++)
- {
- if (!used[i])
- {
- used[i] = true;
- permutate[index] = elements[i];
- Gen(index + 1);
- used[i] = false;
- }
- }
- }
- }
- static int[] variations;
- static void GenVar(int index, int k)
- {
- if (index >= k)
- {
- Console.WriteLine(string.Join(',', variations));
- }
- else
- {
- for (int i = 0; i < elements.Length; i++)
- {
- if (!used[i])
- {
- used[i] = true;
- variations[index] = elements[i];
- GenVar(index + 1, k);
- used[i] = false;
- }
- }
- }
- }
- static void PrintPascalTriangle(int height)
- {
- long[][] triangle = new long[height + 1][];
- for (int row = 0; row < height; row++)
- {
- triangle[row] = new long[row + 1];
- }
- triangle[0][0] = 1;
- for (int row = 0; row < height - 1; row ++)
- {
- for (int col = 0; col <= row; col++)
- {
- triangle[row + 1][col] += triangle[row][col];
- triangle[row + 1][col + 1] += triangle[row][col];
- }
- }
- for (int row = 0; row < height; row++)
- {
- Console.Write("".PadLeft((height - row) * 2));
- for (int col = 0; col <= row; col++)
- {
- Console.Write("{0,3} ", triangle[row][col]);
- }
- Console.WriteLine();
- }
- }
- static void Main(string[] args)
- {
- elements = new int[]{ 1, 2, 3, 4, 5 };
- used = new bool[5];
- permutate = new int[5];
- variations = new int[2];
- GenVar(0, 2);
- PrintPascalTriangle(12);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement