Skip to content

Instantly share code, notes, and snippets.

@takeokunn
Created April 27, 2019 03:01
Show Gist options
  • Save takeokunn/451a3c3a5fdd2dff93199126cf0bf5c4 to your computer and use it in GitHub Desktop.
Save takeokunn/451a3c3a5fdd2dff93199126cf0bf5c4 to your computer and use it in GitHub Desktop.
(defparameter *coins* `(1 5 10 50 100 500))
(defparameter *have-coins* `(3 2 1 3 0 2))
(defparameter *sum* 620)
(defun calc (index sum answer)
(let* ((number (min (floor sum (nth index *coins*)) (nth index *have-coins*)))
(new-sum (- sum (* number (nth index *coins*))))
(new-answer (+ answer number)))
(values new-sum new-answer)))
(defun coins (index sum answer)
(if (equalp index -1)
answer
(multiple-value-bind (new-sum new-answer) (calc index sum answer)
(coins (1- index) new-sum new-answer))))
(defun solve ()
(let ((total (coins 5 *sum* 0)))
(format nil "~D" total)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment