读书人

请问一个Listlt;stringgt;或Listlt;intgt;字符

发布时间: 2013-04-09 16:45:09 作者: rapoo

请教一个List<string>或List<int>字符组合的算法
比如:
List<int> intList=new List<int> ();
intList.Add(1);
intList.Add(2);
intList.Add(3);
intList.Add(4);


需要返回一个字符组合集。如下所示:

1
1,2
1,2,3
1,2,3,4
1,3
1,3,4
1,4
2
2,3
2,3,4
2,4
3
3,4
4

这个算法应该怎么写?

谢谢各位高手!

[解决办法]
你这是一个全排列的问题,代码可以参考下面网页上的代码:
http://blog.csdn.net/LCL_data/article/details/5286847
[解决办法]
30多了,搞了1个多小时才搞出来!


static void Main(string[] args)
{
List<int> intList = new List<int>();
intList.Add(1);
intList.Add(2);
intList.Add(3);
intList.Add(4);

List<string> strList = new List<string>();

for (int i = 0; i < intList.Count; i++)
{
Console.WriteLine(intList[i].ToString());
GetListMethod(intList, i, i+1);
}

Console.ReadKey();
}

public static void GetListMethod(List<int> intList,int n, int m)
{

for (int i = m; i < intList.Count; i++)
{
string s = intList[n].ToString();
for (int j = m; j <=i; j++)
{
s += "," + intList[j].ToString();

}
Console.WriteLine(s);
}
if (m < intList.Count)


{
GetListMethod(intList, n, m + 1);
}
}

读书人网 >C#

热点推荐