假设我有N组数据,然后我从N组数据中选取Y组进行排列组合,得到M中组合。
设N=4,Y=3
则M = (4*3*2)/(3*2*1) 种不重复的组合。
现在我要得到 的是M中组合中每种组合具体包含哪些组。及M种组合分别为
M1:{N1,N2,N3}
M2:{N1,N2,N4}
M3:{N1,N3,N4}
M4:{N2,N3,N4}
我要的就是M1-M4中每一组包含的3组N的数据。附 N组数据中取Y组排列组合,其表达式为
M = (N*(N-1)*(N-2)*....*(N-Y))/(Y*(Y-1)*(Y-2)*...*1)在线等解决方法。
设N=4,Y=3
则M = (4*3*2)/(3*2*1) 种不重复的组合。
现在我要得到 的是M中组合中每种组合具体包含哪些组。及M种组合分别为
M1:{N1,N2,N3}
M2:{N1,N2,N4}
M3:{N1,N3,N4}
M4:{N2,N3,N4}
我要的就是M1-M4中每一组包含的3组N的数据。附 N组数据中取Y组排列组合,其表达式为
M = (N*(N-1)*(N-2)*....*(N-Y))/(Y*(Y-1)*(Y-2)*...*1)在线等解决方法。
跟我的有点类似,但是他有重复的项,我的里面不需要有重复的项。
比如:abc与bca,在他那里面看作是两项。
而我这里面是将其看做一项的。
// 返回值的Count属性表示M的数量
// 每个元素都是bool[]类型,
// 每个[i]表示该组合中是否包含N[i]
// 为方便程序设计,所有下标从0开始
ArrayList M(int n, int y)
{
ArrayList output = new ArrayList();
bool[] nSelected = new bool[n];
GetMs(n, 0, y, nSelected, output);
return output;
}// 根据条件组合剩余的n
// n: 总n; nStart: 从这里开始; yLeft: 剩余要选取的数目,
// nSelected: 之前已经选取的项目; output: 输出
void GetMs(int n, int nStart, int yLeft, bool[] nSelected, ArrayList output)
{
if (nStart >= n)
return;
else if (yLeft == 0)
output.Add(Copy(nSelected));
else
for (; n - nStart >= yLeft; nStart++)
{
nSelected[nStart] = false;
GetMs(n, nStart + 1, yLeft - 1, nSelected, output);
nSelected[nStart] = true;
GetMs(n, nStart + 1, yLeft - 1, nSelected, output);
}
}bool[] Copy(bool[] input) {
bool[] output = new bool[input.Length];
input.CopyTo(output, 0);
return output;
}
{
if (nStart <= n && yLeft == 0)
output.Add(Copy(nSelected));
else if (nStart == n)
return;
else
for (; n - nStart >= yLeft; nStart++)
{
nSelected[nStart] = false;
GetMs(n, nStart + 1, yLeft, nSelected, output);
nSelected[nStart] = true;
GetMs(n, nStart + 1, yLeft - 1, nSelected, output);
nSelected[nStart] = false;
}
}
int M = 10;
int N = 5;
int[] x = new int[5];
ArrayList al = new ArrayList();
int start = N-1;
for(int i=0;i<N;i++)
{
x[i] = i;
}
al.Add(x.Clone());
try
{
while(x[0]<=M-N)
{
for(int i=N-1;i>-1;i--)
{
if(x[i]==M-N+i)
start--;
else
break;
}
int[] temp = (int[])al[al.Count-1];
x[start] = temp[start] + 1;
for(int i=start+1;i<N;i++)
{
x[i] = x[i-1]+1;
}
al.Add(x.Clone());
start = N-1;
}
}
catch
{
}