Advertisement
APXOHT

Untitled

Jan 6th, 2013
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using System;
  2.  
  3. class AllPermutations
  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 Permute(int[] array, int current, int length)
  13.     {
  14.         if (current == length)
  15.         {
  16.             for (int i = 0; i <= length; i++)
  17.             {
  18.                 Console.Write(array[i] + " ");
  19.             }
  20.             Console.WriteLine();
  21.         }
  22.         else
  23.         {
  24.             for (int i = current; i <= length; i++)
  25.             {
  26.                 Swap(ref array[i], ref array[current]);
  27.                 Permute(array, current + 1, length);
  28.                 Swap(ref array[i], ref array[current]);
  29.             }
  30.         }
  31.  
  32.     }
  33.  
  34.     static void Main(string[] args)
  35.     {
  36.         int N = int.Parse(Console.ReadLine());
  37.         int[] arrayOfNumbers = new int[N];
  38.  
  39.         for (int i = 1; i <= N; i++)
  40.         {
  41.             arrayOfNumbers[i - 1] = i;
  42.         }
  43.  
  44.         Permute(arrayOfNumbers, 0, arrayOfNumbers.Length - 1);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement