Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Exam
- {
- class Program
- {
- static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
- {
- if (length == 1) return list.Select(t => new T[] { t });
- return GetPermutations(list, length - 1)
- .SelectMany(t => list.Where(e => !t.Contains(e)),
- (t1, t2) => t1.Concat(new T[] { t2 }));
- }
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- IEnumerable<IEnumerable<int>> result =
- GetPermutations(Enumerable.Range(1, n), n);
- var res = result.ToList();
- for (int i = 0; i < res.Count; i++)
- {
- string output = string.Empty;
- int counter = 0;
- foreach (var item in res[i])
- {
- if (counter == res.Count - 1)
- {
- output += item;
- }
- else
- {
- output += item + " ";
- }
- }
- Console.WriteLine(output);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement