C++典型贪心算法--找硬币问题

本文介绍了一个简单的找零程序实现方案,该程序通过输入所需的找零金额及可用的硬币数量来计算最少硬币数目的找零组合。文章通过示例展示了如何使用C++语言进行逻辑处理,包括对输入数据的读取、找零逻辑的设计以及最终输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/************************************************************************/
/*FileName:Making Change
/*Author:PenglueR
/*Date:2009/07/23
/*Comment:
Problem description
In the old days before everything was electronic, you could pay for stuff with cash and (maybe) get change back. The number of coins you get could vary. For example, 10 cents in change can be done with 1 dime, or 2 nickels, or 1 nickel and 5 pennies, or 10 pennies. The rule of thumb was for the cashier to give the least amount of coins from the selection of coins in the register. Now since the cashiers may not be quick enough to give you the coins, you're to write a program that produces the correct amount of change in the fewest coins

Input
Each line of input will have 5 integers on it. The first number is the amount of change to make. The second number is the number of pennies available, followed by the number of nickels, then the number of dimes and finally the number of quarters.

Output
For each input, print a line that has the number of pennies, the number of nickels, the number of dimes and the number of quarters needed to make the given change using the least number of coins. If the requested amount of change can't be made from the given amount of coins, then print the message "Not enough change".

Sample Input
89 3 0 1 2
89 10 10 10 10
89 0 0 0 4
45 45 0 0 0

Sample Output
Not enough change
4 0 1 3
Not enough change
45 0 0 0
/************************************************************************/

#include <iostream>
using namespace std;
int main()
{
    int money,penny,nickel,dime,quarter,p,n,d,q;
    while (cin>>money>>penny>>nickel>>dime>>quarter||(getchar() != EOF))
    {
        q = money/25;
        if (q <= quarter)
            money -= q*25;
        else
        {
            q = quarter;
            money -= q*25;
        }
        d = money/10;
        if (d <= dime)
            money -= d*10;
        else
        {
            d = dime;
            money -= d*10;
        }
        n = money/5;
        if (n <= nickel)
            money -= n*5;
        else
        {
            n = nickel;
            money -= n*5;
        }
        p = money;
        if (money <=  penny)
            cout<<p<<" "<<n<<" "<<d<<" "<<q<<endl;
        else
            cout<<"Not enough change"<<endl;
    }
    return 0;
}

<think>好的,我现在需要帮助用户了解如何用C语言实现贪心算法。首先,我需要回忆贪心算法的基本概念和特点。贪心算法是一种在每一步选择中都采取当前状态下最优的选择,从而希望导致全局最优解的算法。它常用于解决最优化问题,如霍夫曼编码、最小生成树、迪杰斯特拉算法等。但贪心算法并不总是能得到全局最优解,只有在问题具有贪心选择性质时才能有效。 接下来,我需要考虑用户的具体需求:用C语言实现。所以需要举一个具体的例子,并展示代码结构。常见的例子包括零钱问题、活动选择问题、背包问题等。其中,零钱问题比较直观,适合初学者理解。例如,给定一个金额,用最少的硬币数目来凑出这个金额,假设硬币面额是1、5、10、25等。这个问题符合贪心算法的条件,因为较大的面额可以尽可能多地使用,从而减少总硬币数。 然后,我需要确保代码的正确性。例如,在零钱问题中,需要从最大面额开始,逐步减去该面额的数量,直到金额为零。同时,要注意C语言的语法,比如数组的使用、循环结构等。 另外,用户可能需要了解贪心算法的适用场景和注意事项。比如,贪心算法不一定对所有问题都有效,必须问题具有贪心选择性质。例如,如果硬币面额不同,如包含25、18、1的情况,贪心可能无法得到最优解,这时候需要用动态规划。 接着,根据用户提供的引用内容,虽然引用的内容是关于梯度提升、多任务语言理解和LLaMA模型的,可能与贪心算法无关,但可能需要忽略这些引用,或者寻是否有间接关联的地方。不过看起来这些引用可能是不相关的,所以可能用户提供的引用有误,或者是系统自动添加的。这种情况下,我应该专注于用户的问题本身,即C语言实现贪心算法。 然后,生成相关问题。根据示例,用户需要三个相关问题。例如,贪心算法的适用条件、优化方法,或者与其他算法的比较。 最后,检查是否符合格式要求:行内数学公式用$...$,独立公式用$$...$$,中文回答,代码块使用正确语法,问题部分用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值