Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Combinatorics
- {
- class Program
- {
- static void swap(ref int a, ref int b)
- {
- int temp = a;
- a = b;
- b = temp;
- }
- static void swap(ref char a, ref char b)
- {
- char temp = a;
- a = b;
- b = temp;
- }
- static void Permutate(char[] arr, int n)
- {
- if (n == arr.Length - 1)
- {
- Console.WriteLine(string.Join("", arr));
- return;
- }
- for (int i = n; i < arr.Length; i++)
- {
- swap(ref arr[i], ref arr[n]);
- Permutate(arr, n + 1);
- swap(ref arr[i], ref arr[n]);
- }
- }
- static void Permutate(string str)
- {
- Permutate(str.ToCharArray(), 0);
- }
- static void Permutate(int[] arr, int n)
- {
- if (n == arr.Length - 1)
- {
- Console.WriteLine(string.Join("", arr));
- return;
- }
- for (int i = n; i < arr.Length; i++)
- {
- swap(ref arr[i], ref arr[n]);
- Permutate(arr, n + 1);
- swap(ref arr[i], ref arr[n]);
- }
- }
- static void Permutate(int[] arr)
- {
- Permutate(arr, 0);
- }
- static void Main(string[] args)
- {
- /*int[] arr = { 1, 2, 3, 4, 5 };
- Permutate(arr);*/
- string s = "ABC";
- Permutate(s);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement