对List中元素子集合进行组合排列,使用了递归,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication2
{
class Program
{
static void
{
CombinationsPermutations("asdf", 3);
}
private static void RotateLeft<T>(IList<T> sequence, int start, int count)
{
T tmp = sequence[start];
sequence.RemoveAt(start);
sequence.Insert(start + count - 1, tmp);
}
public static IEnumerable<IList<T>> Combinations<T>(
IList<T> sequence,
int start,
int count,
int choose)
{
if (choose == 0) yield return sequence;
else
{
for (int i = 0; i < count; i++)
{
foreach (var perm in Combinations(
sequence,
start + 1,
count - 1 - i,
choose - 1))
yield return perm;
RotateLeft(sequence, start, count);
}
}
}
public static IEnumerable<IList<T>> Combinations<T>(IList<T> sequence,int choose)
{
return Combinations(sequence, 0, sequence.Count, choose);
}
public static IEnumerable<IList<T>> Permutations<T>(IList<T> sequence, int count)
{
if (count == 1) yield return sequence;
else
{
for (int i = 0; i < count; i++)
{
foreach (var perm in Permutations(sequence, count - 1))
yield return perm;
RotateRight(sequence, count);
}
}
}
public static IEnumerable<IList<T>> Permutations<T>(IList<T> sequence)
{
return Permutations(sequence, sequence.Count);
}
public static void RotateRight<T>(IList<T> sequence, int count)
{
T tmp = sequence[count - 1];
sequence.RemoveAt(count - 1);
sequence.Insert(0, tmp);
}
private static void CombinationsPermutations(string s, int count)
{
foreach (var combo in Combinations(s.ToCharArray().ToList(),count))
{
foreach (var permu in Permutations(combo, count))
{
string r = new string(permu.Take(count).ToArray());
Console.Write("{0,-8}", r);
}
}
Console.WriteLine();
}
}
}
结果如下: