string[] Sample = new string[] { "0", "1", "2", "3", "4"};
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;
}
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;
}