遍历,全排列。
private void Form2_Load(object sender, EventArgs e)
{
int[] abase1=new int[4]{5,6,7,8};
for (int count0 = 4; count0 >= 1; count0--) //取几个数字,比如3个数字相加
{
GetBlockNum(abase1, count0);
}
}
private void GetBlockNum(int[] abase1, int level) {
List<string> basenumAll = new List<string>();
for (int count0 = 4; count0 >= 1; count0--) //第 某 个 情况,比如3个数字相加时的情况
{
List<string> basenum = new List<string>();
GetNextNum(abase1, count0, 0, basenum);
for (int j = 0; j < basenum.Count; j++)
{
basenumAll.Add(basenum[j]);
}
}
Console.Write(basenumAll.Count);
}
private void GetNextNum(int[] abase1, int level, int startPos, List<string> basenum0)
{
StringBuilder sb0 = new StringBuilder(100);
for (int i = startPos; i < 4; i++)
{
if (4 - i >= level)
{
sb0 = new StringBuilder(100);
sb0.Append(abase1[i]);
sb0.Append(",");
if (level > 1)
{
List<string> basenum = new List<string>();
GetNextNum(abase1, level - 1, i + 1, basenum);
for (int j = 0; j < basenum.Count; j++)
{
basenum0.Add(sb0 + basenum[j]);
}
}
else
{
basenum0.Add(sb0.ToString());
}
}
}
}
结果是basenumAll中有所有的排列值
5;
6;
7;
8;
5,6;
5,7;
。。。。。
5,6,7,8;
可以稍微修改后,就是你要的列表了。