using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default15 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int[] el = { 1, 2, 3, 4,5};
this.Response.Write(GetCom<int>(el, 3));
}
public string GetCom<T>(T[] el, int n)
{
string s = "";
int p = 0;
int m = el.Length;
int[] flags = new int[m];
string str = "";
for (int i = 0; i < n; i++)
{
flags[i] = 1;
}
for (int i = n; i < m; i++)
{
flags[i] = 0;
}
p = 0;
for (int i = 0; i < flags.Length; i++)
{
if (flags[i] == 1)
{
p++;
string t = "";
if (p < n)
{
t = ",";
}
s += el[i].ToString()+t;
}
str += flags[i].ToString();
}
str += ";";
s = s + "<br/>";
int count = 1;
bool has10 = false; //是否有"10"组合的标志:true-有;false-无
int bound = 0; //第一个"10"组合的索引
int num1 = 0; //"10"组合左边的"1"的个数
int j;
while (true)
{
num1 = 0;
has10 = false;
for (int i = 0; i < m - 1; i++)
{
if (flags[i] == 1 && flags[i + 1] == 0)//找到第一个"10"组合
{
bound = i;
flags[i] = 0;//将该"10"组合变为"01"组合
flags[i + 1] = 1;
for (j = 0; j < num1; j++)//将其左边的所有"1"全部移动到数组的最左端
{
flags[j] = 1;
}
for (j = num1; j < bound; j++)
{
flags[j] = 0;
}
has10 = true;
break;
}
else if (flags[i] == 1)
{
num1++;
}
}
if (has10 == false)//没有"10"组合了,代表组合计算完毕
{
break;
}
else
{
count++;
}
p = 0;
for (int i = 0; i < flags.Length; i++)
{
if (flags[i] == 1)
{
p++;
string t = "";
if (p < n)
{
t = ",";
}
s += el[i].ToString() + t;
}
str += flags[i].ToString();
}
str += ";";
s = s + "<br/>";
}
string[] sa = str.Split(';');
str ="";
Array.Sort(sa);
for (int i = sa.Length-1; i >0 ; i--)
{
string tempS = "";
if (i > 1)
{
tempS = ";";
}
str += sa[i] + tempS;
}
/*重新排序*/
string[] numA = str.Split(';');
string tempnum = "";
foreach (string ss in numA)
{
for (int jj=0;jj<ss.Length; jj++)
{
if (ss[jj] == '1')
{
tempnum += el[jj].ToString() + ",";
}
}
tempnum += "<br/>";
}
return tempnum;
}
}