实例:换零钱方式的统计

  • 将现金数a换成除第一种硬币之外的所有为他硬币的不同方式数目,加上
  • 将硬币a-d换成所有类型的硬币的不同数目,其中的d是第一种硬币的币值
(define (count-change amount)
  (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-demonination kinds-of-coins))
                       kinds-of-coins)))))
  (define (first-demonination 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)))
  (cc amount 5))
发布了40 篇原创文章 · 获赞 7 · 访问量 1087

猜你喜欢

转载自blog.csdn.net/BobDay/article/details/103643349
今日推荐