1 递归实现字符串的全排列
protected void Page_Load(object sender, EventArgs e)
{
ArrayList strArray = new ArrayList() { "a", "b", "c" ,"d"};
AllArrangement(strArray);
}
//全排列
//递归实现 当前实现需要下一层返回支持
private ArrayList AllArrangement(ArrayList strArray)
{
ArrayList a_Recursion = new ArrayList();//递归数组
ArrayList a_print = new ArrayList();//显示数组
//中止条件 有返回值 或者 无返回值
if (strArray.Count == 2)
{
a_Recursion.Add(strArray[0].ToString() + strArray[1].ToString());
a_Recursion.Add(strArray[1].ToString() + strArray[0].ToString());
return a_Recursion;
}
//生成递归条件
for (int i = 1; i < strArray.Count; i++)
{
a_Recursion.Add(strArray[i]);
}
a_Recursion = AllArrangement(a_Recursion);
//交换并打印 利用返回的数组,与当前层结合,显示排列的字符串
for (int i = 0; i < a_Recursion.Count; i++)// bc cb
{
a_print.Add(strArray[0].ToString() + a_Recursion[i].ToString());
lbl.Text += (++count).ToString()+strArray[0].ToString() + a_Recursion[i].ToString() + " ";
for (int j = 1; j < a_Recursion[i].ToString().Length+1; j++) //bc
{
lbl.Text += count.ToString() + a_Recursion[i].ToString().Insert(j, strArray[0].ToString()) + " ";
a_print.Add(a_Recursion[i].ToString().Insert(j, strArray[0].ToString()));
}
lbl.Text += "<br/>";
}
return a_print;
}