**
** 从m个数中取出n个数的和等于k的组合
**
CLEAR
CREATE CURSOR dd (cc C(254))
m = 7
k = 30
DIMENSION aa[m]
aa[1] = 23
aa[2] = 5
aa[3] = 17
aa[4] = 12
aa[5] = 4
aa[6] = 21
aa[7] = 10
ASORT(aa)
_f(@aa, k)
GO TOP
BROWSE
RETURN
FUNCTION _f(aa, k)
LOCAL i
FOR i = 1 TO ALEN(aa)
IF aa[i] > k
EXIT
ENDIF
ENDFOR
DIMENSION aa[i-1]
FOR i = 1 TO ALEN(aa)
LOCAL bb[i]
_ff(@aa, @bb, i, 1, 1, k)
ENDFOR
ENDFUNC
FUNCTION _ff(aa, bb, n, ai, bi, k)
LOCAL i, dd
IF (n == 0)
dd = bb[1]
ss = TRANSFORM(bb[1])
FOR i = 2 TO bi-1
dd = dd + bb[i]
ss = ss + "," + TRANSFORM(bb[i])
ENDFOR
IF dd == k
INSERT INTO dd VALUES (ss)
ENDIF
RETURN
ENDIF
FOR i = ai TO ALEN(aa) - (n-1)
bb[bi] = aa[i]
_ff(@aa, @bb, n-1, i+1, bi+1, k)
ENDFOR
ENDFUNC
[此贴子已经被作者于2017-6-30 20:36编辑过]