• 华为机试——求最小张数换零钱算法


    C_C++_XY_03.求最小张数换零钱算法

    • 题目描述:

    假设1元,5元,10元,50元,100元的人民币若干,实现一个能找到最少张数累计达到一个指定金额方法。

    如:67元,可分为67个1元钱。也可分为6个10元7个1元

    其中最少人民币分法为一张50元,一张10元,一张5元,两张1元,五张不同金额的拆分方法为最最少张数拆分法

    • 要求实现函数:

    void CalLeastChange(long lInputValue, int *iOutputRlt)

    【输入】 lInputValue: 输入整数金额

    【输出】 lOutputRlt: 输出计算结果

    【注意】仅考虑整数金额拆分方法

    • 示例

    输入:“67”

    输出:“5”

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63

    #include <iostream>
    using namespace std;
     
    void CalLeastChange(long lInputValue, int *iOutputRlt)
    {
        if (lInputValue <= 0)
        {
            *iOutputRlt = 0;
            return;
        }
     
        int count = 0;
        if (lInputValue >= 100)
        {
            while (lInputValue >= 100)
            {
                count++;
                lInputValue -= 100;
            }
        }
     
        if (lInputValue >= 50)
        {
            while (lInputValue >= 50)
            {
                count++;
                lInputValue -= 50;
            }
        }
        if (lInputValue >= 10)
        {
            while (lInputValue >= 10)
            {
                count++;
                lInputValue -= 10;
            }
        }
        if (lInputValue >= 5)
        {
            while (lInputValue >= 5)
            {
                count++;
                lInputValue -= 5;
            }
        }
        if (lInputValue >= 1)
        {
            while (lInputValue >= 1)
            {
                count++;
                lInputValue -= 1;
            }
        }
     
        *iOutputRlt = count;
    }
     
    int main() {
        int iOutputRlt = 0;
        CalLeastChange(67, &iOutputRlt);
        cout << iOutputRlt << endl;
        return 0;
    }
  • 相关阅读:
    ADO.NET Entity Framework如何:通过每种类型多个实体集定义模型(实体框架)
    ADO.NET Entity Framework EDM 生成器 (EdmGen.exe)
    编程之美的求阶乘结果末尾0的个数
    JS 自动提交表单时 报“对象不支持此属性”错误
    php168商务系统品牌无法生成的解决办法
    如何从Access 2000中表删除重复记录
    服务器IUSR_机器名账号找不到怎么办?
    SQL2005 重建全文索引步骤 恢复数据时用到
    PHP页面无法输出XML的解决方法
    bytes2BSTR 解决ajax中ajax.responseBody 乱码问题
  • 原文地址:https://www.cnblogs.com/helloweworld/p/3194211.html
Copyright © 2020-2023  润新知