110,538
社区成员
发帖
与我相关
我的任务
分享
List<int> list = new List<int> { 2, 3, 4, 6 };
List<List<int>> nlist = new List<List<int>>();
for (int i = 0; i < list.Count; i++)
{
if (nlist.Count == 0)
{
nlist.Add(new List<int>());
nlist.Add(new List<int> { list[i] });
continue;
}
int count = nlist.Count;
for (int j = 0; j < count; j++)
{
List<int> xlist = nlist[j].ToList();
nlist[j].Add(list[i]);
nlist.Add(xlist);
}
}
List<List<int>> m = nlist.Where(p => p.Count > 1).ToList();
List<int> list = new List<int> { 2, 3, 4, 6 };
List<List<int>> nlist = new List<List<int>>();
for (int i = 0; i < list.Count; i++)
{
if (nlist.Count == 0)
{
nlist.Add(new List<int>());
nlist.Add(new List<int> { list[i] });
continue;
}
int count = nlist.Count;
for (int j = 0; j < count; j++)
{
List<int> xlist = nlist[j].ToList();
nlist[j].Add(list[i]);
nlist.Add(xlist);
}
}
List<List<int>> m = nlist.Where(p => p.Count > 1).ToList();
static void Main(string[] args)
{
List<int> list = new List<int> { 2, 3, 4, 6 };
List<List<int>> nlist = new List<List<int>>();
for (int i = 0; i < list.Count; i++)
{
if (nlist.Count == 0)
{
nlist.Add(new List<int>());
nlist.Add(new List<int> { list[i] });
continue;
}
int count = nlist.Count;
for (int j = 0; j < count; j++)
{
List<int> xlist = new List<int>();
xlist.AddRange(nlist[j]);
nlist[j].Add(list[i]);
nlist.Add(xlist);
}
}
for (int i = 0; i < nlist.Count; i++)
{
if (nlist[i].Count > 1)
continue;
nlist.RemoveAt(i);
i--;
}
//测试输出
for (int i = 0; i < nlist.Count; i++)
{
int m = 0;
for (int j = 0; j < nlist[i].Count; j++)
{
if (j == nlist[i].Count - 1)
Console.Write("{0} = ", nlist[i][j]);
else
Console.Write("{0} + ", nlist[i][j]);
m += nlist[i][j];
}
Console.Write(m);
Console.WriteLine();
}
Console.ReadLine();
}
for (int i = 0; i < m.Count; i++)
{
for (int j = 0; j < m[i].Count; j++)
{
if (j == m[i].Count - 1)
Console.Write("{0} = ", m[i][j]);
else
Console.Write("{0} + ", m[i][j]);
}
Console.Write(m[i].Sum());
Console.WriteLine();
}
Console.ReadLine();
List<int> list = new List<int> { 2, 3, 4, 6 };
List<List<int>> nlist = new List<List<int>>();
for (int i = 0; i < list.Count; i++)
{
if (nlist.Count == 0)
{
nlist.Add(new List<int>());
nlist.Add(new List<int> { list[i] });
continue;
}
int count = nlist.Count;
for (int j = 0; j < count; j++)
{
List<int> xlist = nlist[j].ToList();
nlist[j].Add(list[i]);
nlist.Add(xlist);
}
}
List<List<int>> m = nlist.Where(p => p.Count > 1).ToList();
int[] arr = new int[] { 2, 3, 4, 6 };
for (int i = 2; i <= arr.Length; i++)
{
List<int[]> lst_Combination = Algorithms.PermutationAndCombination<int>.GetCombination(arr, i);
StringBuilder sb = new StringBuilder();
for (int k = 0; k < lst_Combination.Count; k++)
{
for (int j = 0; j < i; j++)
{
sb.Append(lst_Combination[k][j]);
if (j != i - 1)
sb.Append("+");
else
sb.Append("=");
}
sb.Append(lst_Combination[k].Sum());
sb.Append("\n");
}
Console.Write(sb.ToString());
}
自己稍微做下修改
string[] Sample = new string[] { "2", "3", "4", "6"};
List<string> SampleList = new List<string>();
SampleList.AddRange(Sample);
List<string> result = getCombination(SampleList, 2);
private List<string> getCombination(List<string> SampleList, int m)
{
if (m == 1)
{
return SampleList;
}
List<string> result = new List<string>();
if (SampleList.Count == m)
{
StringBuilder temp_sb = new StringBuilder();
foreach (string s in SampleList)
{
temp_sb.Append(s);
}
result.Add(temp_sb.ToString());
Console.WriteLine(temp_sb.ToString());
return result;
}
string temp_firstelement = SampleList[0];
SampleList.RemoveAt(0);
List<string> temp_samplist1 = new List<string>();
temp_samplist1.AddRange(SampleList);
List<string> temp_list1 = getCombination(temp_samplist1, m - 1);
foreach (string s in temp_list1)
{
result.Add(temp_firstelement + s);
Console.WriteLine(temp_firstelement + s);
}
List<string> temp_samplist2 = new List<string>();
temp_samplist2.AddRange(SampleList);
List<string> temp_list2 = getCombination(temp_samplist2, m);
result.AddRange(temp_list2);
return result;
}