Advertisement
stoianpp

Array 19

Dec 16th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. //* Write a program that reads a number N and generates and prints all the permutations
  2. //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}
  3.  
  4. using System;
  5. class PermutationsOf3
  6. {
  7.     static void Main()
  8.     {
  9.         Console.Write("Enter integer number for permutation: ");
  10.         int loop = int.Parse(Console.ReadLine());
  11.         int[] numArray = new int[loop + 1];
  12.         for (int i = 1; i <= loop; i++) numArray[i] = 1;
  13.         Permutation(1,loop,numArray);
  14.     }
  15.  
  16.     static void Permutation(int loop, int number, int[] array)
  17.     {
  18.         while (array[loop] <= number)
  19.         {
  20.             if (loop < number) Permutation(loop + 1, number, array);
  21.             if (loop == number)
  22.             {
  23.                 Print(array);
  24.             }
  25.             array[loop]++;
  26.         }
  27.         array[loop] = 1;
  28.     }
  29.  
  30.     static void Print(int[] array)
  31.     {
  32.         for (int i = 1; i < array.Length; i++)
  33.         {
  34.             Console.Write("{0,-2}", array[i]);
  35.         }
  36.         Console.WriteLine();
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement