111,094
社区成员




string[] m_Data={"我","是","好","人"};//这里的 数组长度不一定4个,可能 10个 100个
private List<string[]> LS(string[] Data)
{
//...这里不知道怎么写!
}
#region MyRegion 递推
List<List<string>> DoGetAll(List<string> lst) {
List<List<string>> lstRet = new List<List<string>>();
if (lst == null || lst.Count < 2) {
lstRet.Add(lst);
return lstRet;
}
List<List<string>> temp = new List<List<string>>();
for (int i = 0; i < lst.Count - 1; i++) {
List<string> a = lst.GetRange(0, i + 1);
string b = lst[i + 1];
if (i == 0) temp.Add(a);
temp = DoGetAtom(temp, b);
}
return temp;
}
List<List<string>> DoGetAtom(List<List<string>> lst,string b){
List<List<string>> lstRet = new List<List<string>>();
if (lst == null) return lst;
foreach (List<string> sub in lst) {
for(int i = 0; i < sub.Count + 1;i++){
List<string> temp = new List<string>();
temp.AddRange(sub);
temp.Insert(i,b);
lstRet.Add(temp);
}
}
return lstRet;
}
#endregion
public class PermutationCombination
{
/// <summary>
/// 交换两个变量
/// </summary>
/// <param name="a">变量1</param>
/// <param name="b">变量2</param>
public static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
/// <summary>
/// 递归算法求数组的组合(私有成员)
/// </summary>
/// <param name="list">返回的范型</param>
/// <param name="t">所求数组</param>
/// <param name="n">辅助变量</param>
/// <param name="m">辅助变量</param>
/// <param name="b">辅助数组</param>
/// <param name="M">辅助变量M</param>
private static void GetCombination(ref List<int []> list, int [] t, int n, int m, int[] b, int M)
{
for (int i = n; i >= m; i--)
{
b[m - 1] = i - 1;
if (m > 1)
{
GetCombination(ref list, t, i - 1, m - 1, b, M);
}
else
{
int [] temp = new int [M];
for (int j = 0; j < b.Length; j++)
{
temp[j] = t[b[j]];
}
if (GetSum(temp) == 21)
{
list.Add(temp);
}
}
}
}
private static int GetSum(int[] temp)
{
int result = 0;
foreach (int s in temp)
{
result += s;
}
return result;
}
/// <summary>
/// 递归算法求排列(私有成员)
/// </summary>
/// <param name="list">返回的列表</param>
/// <param name="t">所求数组</param>
/// <param name="startIndex">起始标号</param>
/// <param name="endIndex">结束标号</param>
private static void GetPermutation(ref List<int []> list, int [] t, int startIndex, int endIndex)
{
if (startIndex == endIndex)
{
if (list == null)
{
list = new List<int []>();
}
int [] temp = new int [t.Length];
t.CopyTo(temp, 0);
list.Add(temp);
}
else
{
for (int i = startIndex; i <= endIndex; i++)
{
Swap(ref t[startIndex], ref t[i]);
GetPermutation(ref list, t, startIndex + 1, endIndex);
Swap(ref t[startIndex], ref t[i]);
}
}
}
/// <summary>
/// 求从起始标号到结束标号的排列,其余元素不变
/// </summary>
/// <param name="t">所求数组</param>
/// <param name="startIndex">起始标号</param>
/// <param name="endIndex">结束标号</param>
/// <returns>从起始标号到结束标号排列的范型</returns>
public static List<int []> GetPermutation(int [] t, int startIndex, int endIndex)
{
if (startIndex < 0 || endIndex > t.Length - 1)
{
return null;
}
List<int []> list = new List<int []>();
GetPermutation(ref list, t, startIndex, endIndex);
return list;
}
/// <summary>
/// 返回数组所有元素的全排列
/// </summary>
/// <param name="t">所求数组</param>
/// <returns>全排列的范型</returns>
public static List<int []> GetPermutation(int [] t)
{
return GetPermutation(t, 0, t.Length - 1);
}
/// <summary>
/// 求数组中n个元素的排列
/// </summary>
/// <param name="t">所求数组</param>
/// <param name="n">元素个数</param>
/// <returns>数组中n个元素的排列</returns>
public static List<int []> GetPermutation(int [] t, int n)
{
if (n > t.Length)
{
return null;
}
List<int []> list = new List<int []>();
List<int []> c = GetCombination(t, n);
for (int i = 0; i < c.Count; i++)
{
List<int []> l = new List<int []>();
GetPermutation(ref l, c[i], 0, n - 1);
list.AddRange(l);
}
return list;
}
/// <summary>
/// 求数组中n个元素的组合
/// </summary>
/// <param name="t">所求数组</param>
/// <param name="n">元素个数</param>
/// <returns>数组中n个元素的组合的范型</returns>
public static List<int []> GetCombination(int [] t, int n)
{
if (t.Length < n)
{
return null;
}
int[] temp = new int[n];
List<int []> list = new List<int []>();
GetCombination(ref list, t, t.Length, n, temp, n);
return list;
}
}
public List<string> GetFullArray(string str)
{
List<string> list = new List<string>();
if (str.Length < 2)
{
list = new List<string>();
list.Add(str);
return list;
}
for (int i = 0; i < str.Length; i++)
{
List<string> list2 = new List<string>();
list2 = GetFullArray(str.Remove(i, 1));
list = new List<string>();
foreach (string item in list2)
{
for (int j = 0; j < item.Length + 1; j++)
{
list.Add(item.Insert(j, str[i].ToString()));
}
}
}
return list;
}