[題目分析]從n個數中取出所有k個數的所有組合設數已存於數組A[n]中為使結果唯一可以分別求出包括A[n]和不包括A[n]的所有組合即包括A[n]時求出從A[n]中取出k個元素的所有組合不包括A[n]時求出從A[n]中取出k個元素的所有組合
CONST n=k=
TYPE ARR=ARRAY[n] OF integer
VAR ABARR// A中存放n個自然數B中存放輸出結果
PROC outresult//輸出結果
FOR j= TO k DO write(B[j])writeln
ENDP
PROC nkcombination(ijkinteger)
//從i個數中連續取出k個數的所有組合i個數已存入數組A中j為結果數組B中的下標
IF k= THEN outresult
ELSE IF(ik≥)THEN [ B[j]:=A[i]j:=j+
nkcombination(ikj)
nkcombination(ikj)]
ENDP
[算法討論]本算法調用時i是數的個數(題目中的n)k≤ij是結果數組的下標按題中例子用nkcombination()調用若想按正序輸出如…可將條件表達式ik≥改為i+k≤n其中n是數的個數i初始調用時為兩個調用語句中的i均改為i+
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/sjjg/201311/23039.html