日期:2014-05-18 浏览次数:20994 次
static void Main(string[] args)
{
string[] Sample = new string[] { "0", "1", "2", "3", "4", "5","6","7","8","9","a","b","c","d","e","f","g" };
List<string> SampleList = new List<string>();
SampleList.AddRange(Sample);
List<string> result = getCombination(SampleList, 3);
Console.Read();
}
static 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;
}
}
}
------解决方案--------------------
int[] data = { 1, 2, 3, 4 };
var query = from a in data
from b in data
select a.ToString() + "," + b.ToString();
foreach (var item in query)
{
Console.WriteLine(item);
}
------解决方案--------------------
http://www.cnblogs.com/rogerwei/archive/2010/11/18/1880336.html
------解决方案--------------------
写一个通用的:
IEnumerable<IEnumerable<T>> Combo<T>(IEnumerable<<IEnumerable<T>> source, IEnumerable<T> data)
{
return data.SelectManay(x => source.Select(y => y.Concat(new T[] { x })));
}
------解决方案--------------------
int[] data = { 1, 2, 3, 4 };
private void SetData(int start,int count)
{
if (start == count - 1) return;
for (int i = start+1; i < count; i++)
{
Debug.WriteLine(data[start].ToString() + "," + data[i].ToString());
listBox1.Items.Add(data[start].ToString() + "," + data[i].ToString());
}
SetImage(start + 1, data.Length);
}
使用
SetData(0, data.Length);
/*
1,2
1,3
1,4
2,3
2,4
3,4
*/