- static void Swap<T>(ref T a, ref T b)
- {
- T t = a;
- a = b;
- b = t;
- }
- static IEnumerable<int[]> Perm(int[] arr, int pos)
- {
- if (pos == arr.Length)
- {
- yield return arr;
- }
- for (int i = pos; i < arr.Length; ++i)
- {
- Swap(ref arr[i], ref arr[pos]);
- foreach (var j in Perm(arr, pos + 1)) yield return j;
- Swap(ref arr[i], ref arr[pos]);
- }
- }
- static void Main(string[] args)
- {
- foreach (var i in Perm(new int[] { 1, 2, 3, 4 }, 0))
- {
- Console.WriteLine(string.Join(",", i.Select(j=>j.ToString()).ToArray()));
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/081020136282.html
来源: http://www.codesnippet.cn/detail/081020136282.html