zoukankan      html  css  js  c++  java
  • 换零钱的方法

    现有面值分别为 1分, 5分, 10分, 25分, 50分 的五种硬币, 问一枚一块钱的硬币换成这几种硬币有几种换法。

    先要明确一个事实, 总的换法一定等于 使用某一种硬币的换法和不使用某一种硬币的换法之和。

    前者需要先将总数减去一定使用的那种硬币的面值, 再来讨论剩下的兑换方法。

    后者需要先将硬币的种类减一, 再来讨论剩下硬币的兑换方法。

    而归约的最终结果无非三种:

    硬币总数为 0,    则返回一种方案。

    硬币总数小于 0, 则返回 0 种方案。

    硬币种类为0,     则返回 0 种方案。

    schme 实现:

    (define (count-change amount)
      (cc amount 5))
    
    (define (cc amount kinds-of-coins)
      (cond ((= amount 0) 1)
               ((or (< amount 0) (< kinds-of-coins 1)) 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)))
    
    (count-change 100)

    C++ 实现:

    #include <iostream>
    
    using namespace std;
    
    int FirstDenomination (const int &kinds_of_coins)
    {
        switch (kinds_of_coins) {
        case 1: return 1;
        case 2: return 5;
        case 3: return 10;
        case 4: return 25;
        default: return 50;
        }
    }
    
    int CountChange (const int &amount
                     , const int &kinds_of_coins)
    {
        if (amount == 0) {
            return 1;
        }
        else if (amount < 0 || kinds_of_coins < 1) {
            return 0;
        }
        else {
            auto rest_kinds_of_coins = kinds_of_coins - 1;
            auto rest_amount = 
                amount - FirstDenomination (kinds_of_coins);
            return ( CountChange(amount
                                 , rest_kinds_of_coins)
                   + CountChange(rest_amount
                                 , kinds_of_coins));
        }
    }
    
    int WaysToChange (const int &amount)
    {
        return move(CountChange (amount, 5));
    }
    
    int main ()
    {
        cout << move(WaysToChange (100));
        cout << endl;
        return 0;
    }
  • 相关阅读:
    python笔记-列表和元组
    T-sql for xml path使用(转)
    除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
    VS2015 经常不出现智能提示,代码颜色也没有了
    虚拟机重新决定网卡地址时,老是报错
    模块的命令
    关闭NetworkManager的作用
    centos6上yum安装drbd(内核:2.6.32.696)
    检查硬件变化的命令kudzu
    parted分区和挂载及非交互式操作
  • 原文地址:https://www.cnblogs.com/wuOverflow/p/4265812.html
Copyright © 2011-2022 走看看