Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //* Write a program that reads a number N and generates and prints all the permutations
- //of the numbers [1 … N]. Example:n = 3 {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}
- using System;
- class PermutationsOf3
- {
- static void Main()
- {
- Console.Write("Enter integer number for permutation: ");
- int loop = int.Parse(Console.ReadLine());
- int[] numArray = new int[loop + 1];
- for (int i = 1; i <= loop; i++) numArray[i] = 1;
- Permutation(1,loop,numArray);
- }
- static void Permutation(int loop, int number, int[] array)
- {
- while (array[loop] <= number)
- {
- if (loop < number) Permutation(loop + 1, number, array);
- if (loop == number)
- {
- Print(array);
- }
- array[loop]++;
- }
- array[loop] = 1;
- }
- static void Print(int[] array)
- {
- for (int i = 1; i < array.Length; i++)
- {
- Console.Write("{0,-2}", array[i]);
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement