Advertisement
Guest User

FatimaO2

a guest
May 5th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication7
  8. {
  9.     class Program
  10.     {
  11.             static void Main(string[] args)
  12. {
  13.     string str = "ABC";
  14.     char[] charArry = str.ToCharArray();
  15.     permute(charArry, 0, 2);
  16.     Console.ReadKey();
  17. }
  18.  
  19. static void permute(char[] arry, int i, int n)
  20. {
  21.     int j;
  22.     if (i==n)
  23.         Console.WriteLine(arry);
  24.     else
  25.     {
  26.         for(j = i; j <=n; j++)
  27.         {
  28.             swap(ref arry[i],ref arry[j]);
  29.             permute(arry,i+1,n);
  30.             swap(ref arry[i], ref arry[j]); //backtrack
  31.         }
  32.     }
  33. }
  34.  
  35. static void swap(ref char a, ref char b)
  36. {
  37.     char tmp;
  38.     tmp = a;
  39.     a=b;
  40.     b = tmp;
  41. }
  42.         }
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement