Advertisement
stanevplamen

02.1.19.NumberPermutations

May 22nd, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using System;
  2.  
  3. class NumberPermutations
  4. {
  5.     static void Swap(ref int first, ref int second)
  6.     {
  7.         int temp = first;
  8.         first = second;
  9.         second = temp;
  10.     }
  11.  
  12.     static void Permutations(int[] vector, int numbersLength, int start)
  13.     {
  14.  
  15.         if (start == numbersLength)
  16.         {
  17.             Print(vector);
  18.         }
  19.         else
  20.         {
  21.             for (int i = start; i <= numbersLength; i++)
  22.             {
  23.                 Swap(ref vector[i], ref vector[start]);
  24.                 Permutations(vector, numbersLength, start + 1);
  25.                 Swap(ref vector[i], ref vector[start]);
  26.             }
  27.         }
  28.     }
  29.  
  30.     static void LoadVector(int[] vector, int vectorLenght)
  31.     {
  32.         for (int i = 0; i < vectorLenght; i++)
  33.         {
  34.             vector[i] = i + 1;
  35.         }
  36.         Permutations(vector, vectorLenght-1, 0);
  37.     }
  38.  
  39.     static void Print(int[] vector)
  40.     {
  41.         foreach (int i in vector)
  42.         {
  43.             Console.Write("{0} ", i);
  44.         }
  45.         Console.WriteLine();
  46.     }
  47.  
  48.     static void Main()
  49.     {
  50.         Console.Write("Please enter the amount of the numbers 1...N, N = ");
  51.         int numberN = int.Parse(Console.ReadLine());
  52.         int[] vector = new int[numberN];
  53.         LoadVector(vector, numberN);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement