原理如圖:
算法如下:
private function compoud(N:uint, C:uint):void
{
var pickIndex:int = 0;
var X:int=1;
var Y:int=1;
var P:int=1;
var i:int=0;
var arr:Array = new Array();
var src:Array = new Array();
for(i=0;i<N;i++)
{
src.push(i+1);
}
while(true)
{
if(pickIndex<N)
{
arr.push(src[pickIndex++]);
if(arr.length==C)
{
var tmp:String = "";
for(i=0;i<C;i++)
{
tmp +=arr[i]+ " ";
}
trace(tmp);
if(pickIndex==N)
{
var backto:int =-1;
for(i=C-1;i>0;i--)
{
if(arr[i]-arr[i-1]>1)
{
backto = i-1;
break;
}
}
if(backto==-1)
{
break;
}
pickIndex = arr[backto];
for(i=0;i<C-backto;i++)
{
arr.pop();
}
}
else
{
arr.pop();
}
}
}
else
{
break;
}
}
}
摘自 aerror的專欄