本帖最后由 kkq0305 于 2021-8-13 11:51 编辑
- (defun combin (lis n);lis表,n为选出个数。
- (cond ((= n 0) nil)
- ((= n 1) (mapcar 'list lis))
- ((= n (length lis)) (list lis))
- ((> n (length lis)) (alert "combin系数大于表元素数"))
- (t (append (combin (cdr lis) n)
- (mapcar '(lambda (x) (cons (car lis) x))
- (combin (cdr lis) (1- n)))))))
使用递归算法把组合简化
示例:(combin '(1 2 3 4 5 6) 2) ((5 6) (4 5) (4 6) (3 4) (3 5) (3 6) (2 3) (2 4) (2 5) (2 6) (1 2) (1 3) (1 4) (1 5) (1 6))
|