money-exchange calculates combinations of how to change a larger coin into smaller coins
This commit is contained in:
parent
1b3d763cc6
commit
9a7a74f952
14
money-exchange.scm
Normal file
14
money-exchange.scm
Normal file
@ -0,0 +1,14 @@
|
||||
(define (change amount kinds-of-coins)
|
||||
(cond ((or (< amount 0) (= kinds-of-coins 0)) 0)
|
||||
((= amount 0) 1)
|
||||
(else (+ (change amount (- kinds-of-coins 1))
|
||||
(change (- amount (amount-wo-largest-coin kinds-of-coins)) kinds-of-coins)))))
|
||||
|
||||
(define (amount-wo-largest-coin coins-left)
|
||||
(cond ((= coins-left 5) 50)
|
||||
((= coins-left 4) 25)
|
||||
((= coins-left 3) 10)
|
||||
((= coins-left 2) 5)
|
||||
((= coins-left 1) 1)))
|
||||
|
||||
(print (change 100 5))
|
@ -19,4 +19,4 @@
|
||||
|
||||
(define (sqrt x) (sqrt_iter (/ x 2.0) x x))
|
||||
|
||||
(print (sqrt 2))
|
||||
(print (sqrt 10))
|
||||
|
Loading…
Reference in New Issue
Block a user