答复: 有1元,5元,10元,20元,50元,问组成100元有多少种情况

SICP, 1.2.2  Tree Recursion
有详细的解释
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.2

CLISP的一种实现

(defparameter us-coins (list 50 25 10 5 1))

(defun no-more? (coin-values)
  (null coin-values))

(defun except-first-denomination (coin-values)
  (cdr coin-values))

(defun first-denomination (coin-values)
  (car coin-values))

(defun cc (amount coin-values)
  (cond ((= amount 0) 1)
        ((or (< amount 0) (no-more? coin-values)) 0)
        (t
         (+ (cc amount
                (except-first-denomination coin-values))
            (cc (- amount
                   (first-denomination coin-values))
                coin-values)))))

(defun count-change (amount &optional (coin-values us-coins))
  (cc amount coin-values))

;;; test, assertEquals 343
(print (count-change 100 '(50 20 10 5 1)))

你可能感兴趣的:(html)