poj 2581 Exact Change Only (dp)

本文介绍了一个经典的编程问题——精确找零。通过简单的for循环或动态规划解决如何使用最少数量的硬币来达到指定金额的问题。

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



Description

Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. "Where we at?" Thibodeaux groggily yawned. 

"Not in Vegas, I gua-ran-tee, but could you get my knapsack?" Boudreaux asked, gesturing to the worn, leather backpack in the back seat of their cherry red Ford Miata. 

"Why, is there a problem?" 

"Just hand me my knapsack, problem or not." 

Thibodeaux complied, glancing up as Boudreaux slowed the car to a stop in a line of vehicles approaching a toll booth. "$1.65 -- Exact change only," Thibodeaux read the yellow sign on the front of a small wooden building occupied by a lone toll booth operator. "I have to get $1.65 in exact change?" Thibodeaux asked, digging through the knapsack, "all I have are ten quarters, four dimes, and three pennies. I don't have any nickels . . ." 

"Just give me five of the quarters and the four dimes," Boudreaux replied, holding out his hand. 

"Oh yeah," Thibodeaux said, handing over the coins, "that does add up to $1.65. I wish there were an easy way to figure out if you have an exact monetary amount, given a set of coins." 

"Hmmm," Boudreaux shrugged, "sounds like a good programming problem." 
 

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. 

A single data set has 1 component: 

Start line - A single line: 
         A B C D E 

where: 
     A: (0.01 ≤ A ≤ 5.00) is a decimal number (to two decimal places) of a monetary amount. 
     B: (0 ≤ B ≤ 100) is an integer number of quarters (one quarter = $0.25). 
     C: (0 ≤ C ≤ 100) is an integer number of dimes (one dime = $0.10). 
     D: (0 ≤ D ≤ 100) is an integer number of nickels (one nickel = $0.05). 
     E: (0 ≤ E ≤ 100) is an integer number of pennies (one penny = $0.01). 
 

Output

For each data set, there will be exactly one line of output. If there exists one or more subsets of the given coins whose values add up to the given monetary amount exactly, the output will be a single line in the form: 

    A B C D 

where A is the number of quarters, B is the number of dimes, C is the number of nickels, and D is the number of pennies, for the subset with the fewest number of coins. Otherwise, the output will be a single line with the statement: 

    NO EXACT CHANGE 
 

Sample Input

0.45 2 1 1 4 0.75 3 7 1 75
 

Sample Output

NO EXACT CHANGE 3 0 0 0 
题意及分析:
好久不写博客了,据学长说,写博客是一个好习惯。哎,,,
这个题可以用for循环直接过,用dp的话,当时听人说要保存路径什么的,,还是胆子小了,不敢自己尝试。应该自己多试试。
AC代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
#define eps 1e-9
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
#define M 1000+10
#define MAXINT (1<<31)-1
using namespace std;
int dp[1000],pre[1000];
int main()
{
    int a[10]={0};
    int c[10]={0,25,10,5,1};
    int i,j;
    double tot;

    while(~scanf("%lf",&tot))
    {
        for(i=1;i<=4;i++)
            scanf("%d",&a[i]);
        int sum=(int)(tot*100+0.5);
        memset(dp,0xcf,sizeof(dp));   //刚开始,不知道dp怎么保证总的数量最小,其实物品的价值是从大到小的,只要选择前面的物品,最后的                                //
        dp[0]=0;                        //数量就是最小的,因为前面的物品价值大。
        for(i=1;i<=4;i++)
        {
            if(a[i]*c[i]>=sum)
            {  
                for(j=c[i];j<=sum;j++)
                    if((dp[j]=c[i];j--)
                        if((dp[j]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值