POJ1787Charlie's Change

Charlie‘s Change

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 2978   Accepted: 844

Description

Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motorests. Charlie hates change. That is basically the setup of your next task.

Your program will be given numbers and types of coins Charlie has and the coffee price. The coffee vending machines accept coins of values 1, 5, 10, and 25 cents. The program should output which coins Charlie has to use paying the coffee so that he uses as
many coins as possible. Because Charlie really does not want any change back he wants to pay the price exactly.

Input

Each line of the input contains five integer numbers separated by a single space describing one situation to solve. The first integer on the line P, 1 <= P <= 10 000, is the coffee price in cents. Next four integers, C1, C2, C3,
C4, 0 <= Ci <= 10 000, are the numbers of cents, nickels (5 cents), dimes (10 cents), and quarters (25 cents) in Charlie‘s valet. The last line of the input contains five zeros and no output should be generated for it.

Output

For each situation, your program should output one line containing the string "Throw in T1 cents, T2 nickels, T3 dimes, and T4 quarters.", where T1, T2, T3, T4 are the numbers of coins of appropriate values Charlie should use to
pay the coffee while using as many coins as possible. In the case Charlie does not possess enough change to pay the price of the coffee exactly, your program should output "Charlie cannot buy coffee.".

Sample Input

12 5 3 1 2
16 0 0 0 1
0 0 0 0 0

Sample Output

Throw in 2 cents, 2 nickels, 0 dimes, and 0 quarters.
Charlie cannot buy coffee.

Source

题意:给定一个总金额,然后分别是1,5,10,25每种硬币的数量,问最多可以拿多少硬币恰好支付

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
const int MX=10000;
using namespace std;
int main()
{
    int i,j,k,g[4],v,c[4]={1,5,10,25},dp[11000][5];//dp[.][0]表示最多的总硬币,dp[.][1~4]分别表示每种硬币的数量
    while(1)
    {
        int s=0;
        scanf("%d",&v);
        s+=v;
        for(i=0;i<4;i++)
        {
            scanf("%d",g+i);
            s+=g[i]*c[i];
        }
        //cout<<s<<endl;
        if(s==0)break;
        s-=v;
        if(s<v)
        {
            printf("Charlie cannot buy coffee.\n");
        }else if(s==v){
            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",g[0],g[1],g[2],g[3]);
        }else{
            memset(dp,-MX,sizeof dp);
            for(i=0;i<5;i++)
            dp[0][i]=0;
            for(i=0;i<4;i++)
            {
                k=1;
                while(g[i]-k>=0){
                   for(j=v;j>=c[i]*k;j--)
                   {
                       if(dp[j][0]<dp[j-c[i]*k][0]+k)
                       {
                           dp[j][0]=dp[j-c[i]*k][0]+k;
                           for(int x=1;x<5;x++)
                           dp[j][x]=dp[j-c[i]*k][x];
                           dp[j][i+1]+=k;
                       }
                   }
                   g[i]-=k;
                   k<<=1;
                }
                if(g[i]>0)
                {
                    k=g[i];
                   for(j=v;j>=c[i]*k;j--)
                   {
                       if(dp[j][0]<dp[j-c[i]*k][0]+k)
                       {
                           dp[j][0]=dp[j-c[i]*k][0]+k;
                           for(int x=1;x<5;x++)
                           dp[j][x]=dp[j-c[i]*k][x];
                           dp[j][i+1]+=k;
                       }
                   }
                }
            }
            for(i=0;i<5;i++)
            {
                if(dp[v][i]<0)
                {
                    printf("Charlie cannot buy coffee.\n");break;
                }
            }
            if(i==5){
            printf("Throw in %d cents, %d nickels, %d dimes, and %d quarters.\n",dp[v][1],dp[v][2],dp[v][3],dp[v][4]);
            }
        }
    }
    return 0;
}

POJ1787Charlie's Change

时间: 2024-07-31 21:20:40

POJ1787Charlie's Change的相关文章

poj1787Charlie&#39;s Change(多重背包+记录路径+好题)

Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3720   Accepted: 1125 Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffee at coffee vending machines at motore

centos yum安装ftp 及解决vsftp错误500 OOPS: cannot change directory:/home/**

centos yum安装ftp 及解决vsftp错误500 OOPS: cannot changedirectory:/home/**   1.查看服务器有没有安装ftp包 rpm -qa |grep vsftp 2.yum  安装 yum -y install vsftpd 3.启动vsftp的服务 service vsftpd start 4.关闭防火墙 service iptables stop 5.测试匿名访问 修改配置文件 vsftpd.conf [[email protected]

How to change SMTP Banner, HELO,EHLO Greetings for Zimbra

1- Take Zimbra backup or VM snapshot before doing any changes 2- Enter the following command to change SMTP Banner: # su - zimbra $ zmlocalconfig -e postfix_smtpd_banner="yourmailserver.yourdomain.com" 3- Then if you want to change HELO/EHLO nam

【oracle】oracledba4 when you need to change storage options

In which scenarios would you rebuild an index? (Choose all that a pply.) A. when you need to disable the index usage B. when you need to change storage options C. when you need to enable index monitoring D. when you need to move the index to another 

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

eclipse中,项目有红叉之-Cannot change version of project facet Dynamic Web Module to 3.1

1.打开Problems查看错误原因Window->Show View->Other->General->Problems 2.查看问题 3.发现是Cannot change version of project facet Dynamic Web Module to 3.1 4.根据2里截图的Resource,得到是哪个项目,项目右击->Properties->Project Faces->Dynamic Web Module,更据相应Dynamic Web M

Linux指令类型(一)change指令

一.change指令 chattr chgrp chmod chown chfn chsh chroot 二.ch指令详细介绍 (1)chattr 全名:change attribute 作用:chattr命令用于改变文件属性 语法:chattr [-RV][-v<版本编号>][+/-/=<属性>][文件或目录...] 参数: -R 递归处理,将指定目录下的所有文件及子目录一并处理. -v<版本编号> 设置文件或目录版本. -V 显示指令执行过程. +<属性>

多个jdk 变更 引起 tomcat插件 启动不了 The JRE could not be found.Edit the server and change the JRE location.

The JRE could not be found.Edit the server and change the JRE location. 在Windows->Preferences->Server->Runtime Environments 选择Tomcat->Edit,在jre中选择相应的jdk版本,完事.

[转]解决Cannot change version of project facet Dynamic web module to 2.5

我们用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一般现在至少都是2.5,在Project Facets里面修改Dynamic web module为2.5的时候就会出现Cannot change version of project facet Dynamic web module to 2.5,如图: 其实在右边可以看到改到2.5需要的条件以及有