給了50,25,10,5,1美分的硬幣,將1美圓(100美分)換成零錢,一共有多少方式?code
採用遞歸,假定咱們所考慮的可用硬幣類型種類排了某種順序:那麼將總數爲a的現金換成n種硬幣的不一樣方式的數目等於:遞歸
換一種方式來說,即方式的數目等於io
對於1來講,咱們排除了第一種硬幣的兌換方法;對於2來講,咱們將總金額減小了d。方法
(define (count-change amount) (cc amount 5)) (define (cc amount kinds-of-coins) (cond ((= amount 0) 1) ((or (< amount 0) (= kinds-of-coins 0)) 0) (else (+ (cc amount (- kinds-of-coins 1)) (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins))))) (define (first-denomination kinds-of-coins) (cond ((= kinds-of-coins 1) 1) ((= kinds-of-coins 2) 5) ((= kinds-of-coins 3) 10) ((= kinds-of-coins 4) 25) ((= kinds-of-coins 5) 50)))