|
[code]// 指定组合数量递归函数模板(1 2 3) n=1 => (1) (2) (3) n=2 => (1 2) (1 3) (2 3)
template <class T>
static AcArray<T> combination(T arr,int m)
{
AcArray<T> arrs;
if (arr.length()>0 && m > 0)
{
T cdr=arr;
cdr.removeFirst();
AcArray<T> tmparrs1=combination(cdr,m-1);
if (tmparrs1.isEmpty() && m ==1)
{
T tmp;
tmp.append(arr.first());
arrs.append(tmp);
}
else
{
for (int i=0;i<tmparrs1.length();i++)
{
tmparrs1.at(i).insertAt(0,arr.first());
}
arrs.append(tmparrs1);
}
AcArray<T> tmparrs2 = combination(cdr,m);
if (!tmparrs2.isEmpty())
{
arrs.append(tmparrs2);
}
}
return arrs;
}
//全部组合函数模板(1 2 3) => (1) (2) (3) (1 2) (1 3) (2 3) (1 2 3)
template <class T>
static AcArray<T> combination(T arr)
{
AcArray<T> arrs;
for (int i=1;i<=arr.length();i++)
{
//调用递归
AcArray<T> tempArray =combination(arr,i);
arrs.append(tempArray);
}
return arrs;
}[/code] |
|