对List中元素进行全排列,使用了递归,代码如下:
class Program
{
static void
{
List<int> list = new List<int>() { 6,7,8};
foreach (var p
{
foreach (var i in p1)
Console.Write(i.ToString() + " ");
Console.WriteLine();
}
string strTest = "
foreach (List<char> p
{
string strTmp = new string(p2.ToArray());
Console.Write(strTmp + "\t");
}
Console.WriteLine();
}
public static void CircleRight(IList seq, int count)
{
object tmp = seq[count - 1];
seq.RemoveAt(count - 1);
seq.Insert(0, tmp);
}
public static IEnumerable<IList> Permutate(IList seq, int count)
{
if (count == 1) yield return seq;
else
{
for (int i = 0; i < count; i++)
{
foreach (var perm in Permutate(seq, count - 1))
yield return perm;
CircleRight(seq, count);
}
}
}
}
结果如下: